-- ════════════════════════════════════════════════════════════
-- مُصادَق — Phase 1: AI Usage Tracking + Notifications
-- ════════════════════════════════════════════════════════════

-- AI Usage Log (tracks every AI request)
CREATE TABLE IF NOT EXISTS ai_usage_log (
    id              INT AUTO_INCREMENT PRIMARY KEY,
    tenant_id       CHAR(36) NOT NULL,
    user_id         CHAR(36) NULL,
    company_id      CHAR(36) NULL,
    action_type     ENUM('invoice_extraction','voice_transcribe','voice_intent','report_generation','chatbot') NOT NULL,
    model_name      VARCHAR(50) NOT NULL,
    prompt_tokens   INT DEFAULT 0,
    completion_tokens INT DEFAULT 0,
    total_tokens    INT DEFAULT 0,
    estimated_cost  DECIMAL(10,6) DEFAULT 0,
    request_metadata JSON NULL,
    created_at      DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_tenant_date (tenant_id, created_at),
    INDEX idx_action (action_type),
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Notifications
CREATE TABLE IF NOT EXISTS notifications (
    id              CHAR(36) PRIMARY KEY DEFAULT (UUID()),
    tenant_id       CHAR(36) NOT NULL,
    user_id         CHAR(36) NULL,
    type            ENUM('invoice_processed','invoice_rejected','quota_warning','month_end','system','achievement') NOT NULL,
    title           VARCHAR(255) NOT NULL,
    body            TEXT NOT NULL,
    is_read         BOOLEAN DEFAULT FALSE,
    metadata        JSON NULL,
    created_at      DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_user_read (user_id, is_read),
    INDEX idx_tenant (tenant_id),
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Referral Codes (Phase 2 prep)
CREATE TABLE IF NOT EXISTS referral_codes (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    user_id     CHAR(36) NOT NULL,
    code        VARCHAR(20) NOT NULL UNIQUE,
    uses_count  INT DEFAULT 0,
    max_uses    INT DEFAULT 50,
    reward_months INT DEFAULT 1,
    is_active   BOOLEAN DEFAULT TRUE,
    created_at  DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Referral Uses (Phase 2 prep)
CREATE TABLE IF NOT EXISTS referral_uses (
    id                  INT AUTO_INCREMENT PRIMARY KEY,
    code_id             INT NOT NULL,
    referred_tenant_id  CHAR(36) NOT NULL,
    reward_applied      BOOLEAN DEFAULT FALSE,
    created_at          DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (code_id) REFERENCES referral_codes(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- User Achievements (Phase 2 prep)
CREATE TABLE IF NOT EXISTS user_achievements (
    id                  INT AUTO_INCREMENT PRIMARY KEY,
    user_id             CHAR(36) NOT NULL,
    achievement_code    VARCHAR(50) NOT NULL,
    points              INT NOT NULL DEFAULT 0,
    earned_at           DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    UNIQUE KEY uq_user_achievement (user_id, achievement_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
