-- Plans table
CREATE TABLE IF NOT EXISTS plans (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    price DECIMAL(10, 2) DEFAULT 0.00,
    max_customers INT DEFAULT 10,
    can_custom_domain BOOLEAN DEFAULT FALSE,
    is_trial BOOLEAN DEFAULT FALSE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Seed default plans
INSERT INTO plans (name, price, max_customers, can_custom_domain, is_trial) VALUES
('Free Trial', 0.00, 5, FALSE, TRUE),
('Pro', 49.00, 50, TRUE, FALSE),
('Enterprise', 199.00, 1000, TRUE, FALSE);

-- MSPs table (Tenants)
CREATE TABLE IF NOT EXISTS msps (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    plan_id INT NOT NULL,
    subdomain VARCHAR(255) UNIQUE,
    custom_domain VARCHAR(255) UNIQUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (plan_id) REFERENCES plans(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Users table
CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    msp_id INT NULL, -- NULL for superadmins
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) UNIQUE NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    role ENUM('superadmin', 'msp_admin', 'msp_staff') NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (msp_id) REFERENCES msps(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Onboarding Templates
CREATE TABLE IF NOT EXISTS onboarding_templates (
    id INT AUTO_INCREMENT PRIMARY KEY,
    msp_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (msp_id) REFERENCES msps(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Onboarding Template Steps (Pages)
CREATE TABLE IF NOT EXISTS onboarding_template_steps (
    id INT AUTO_INCREMENT PRIMARY KEY,
    template_id INT NOT NULL,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    order_index INT DEFAULT 0,
    FOREIGN KEY (template_id) REFERENCES onboarding_templates(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Onboarding Template Fields/Content
CREATE TABLE IF NOT EXISTS onboarding_template_fields (
    id INT AUTO_INCREMENT PRIMARY KEY,
    step_id INT NOT NULL,
    type ENUM('text', 'form_input', 'form_textarea', 'form_select', 'download', 'upload') NOT NULL,
    label VARCHAR(255) NOT NULL,
    content TEXT, -- Used for 'text' or 'download' URL or 'options' for select
    is_required BOOLEAN DEFAULT FALSE,
    order_index INT DEFAULT 0,
    FOREIGN KEY (step_id) REFERENCES onboarding_template_steps(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Update customers table to link to MSP
ALTER TABLE customers ADD COLUMN msp_id INT NOT NULL AFTER id;
ALTER TABLE customers ADD CONSTRAINT fk_customer_msp FOREIGN KEY (msp_id) REFERENCES msps(id) ON DELETE CASCADE;

-- Create a default superadmin (password: admin123)
-- In a real app, this would be handled via a setup script or CLI.
INSERT INTO users (name, email, password_hash, role) VALUES
('Super Admin', 'admin@msp-portal.com', '$2y$10$GCGS7B2IaiORPL9MtntJ1uw4QiuzpQgqvexHwRu/CI8M3Lmtex5BS', 'superadmin');
