-- ============================================================================
-- EduTrack — Demo gating schema + dedicated demo account
-- Run this ONCE on the demo server (SCHOOL_CODE = DEMO).
--   mysql -u <user> -p <db> < database/demo_access.sql
-- The demo_requests table is also auto-created at runtime, but importing here
-- lets you seed the demo user and the WhatsApp setting in one shot.
-- ============================================================================

CREATE TABLE IF NOT EXISTS demo_requests (
    id INT AUTO_INCREMENT PRIMARY KEY,
    school_name     VARCHAR(200) NOT NULL,
    contact_name    VARCHAR(150) NOT NULL,
    role            VARCHAR(100) DEFAULT NULL,
    whatsapp        VARCHAR(40)  NOT NULL,
    email           VARCHAR(190) DEFAULT NULL,
    region          VARCHAR(120) DEFAULT NULL,
    message         TEXT,
    status          ENUM('pending','approved','rejected') NOT NULL DEFAULT 'pending',
    access_code     VARCHAR(20)  DEFAULT NULL,
    code_expires_at DATETIME     DEFAULT NULL,
    uses_allowed    INT DEFAULT 50,
    uses_count      INT DEFAULT 0,
    ip_address      VARCHAR(45)  DEFAULT NULL,
    created_at      DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    approved_at     DATETIME DEFAULT NULL,
    last_access_at  DATETIME DEFAULT NULL,
    INDEX idx_status (status),
    INDEX idx_code (access_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Team WhatsApp number used on the request page + approval deep-links.
INSERT INTO settings (setting_key, setting_value)
VALUES ('demo_whatsapp', '233537793106')
ON DUPLICATE KEY UPDATE setting_value = setting_value;

-- ----------------------------------------------------------------------------
-- Dedicated, code-only DEMO showcase account.
-- Password below is a random throwaway: visitors NEVER type it (they enter via
-- an access code), and ajax/login.php blocks this username from password login
-- while DEMO_MODE is on. Change the hash if you like; it does not matter.
-- Hash = password_hash('demo-no-direct-login-'.random, PASSWORD_BCRYPT)
-- ----------------------------------------------------------------------------
INSERT INTO users (username, password, full_name, role)
VALUES (
    'demo',
    '$2y$12$h9tJt3tAojsds0qVfEUXseupUr1ik1lUn/GtIHyy9Qb0uCnVtvI.K',
    'Demo School (Showcase)',
    'admin'
)
ON DUPLICATE KEY UPDATE full_name = VALUES(full_name);
