CREATE DATABASE IF NOT EXISTS datandes_cms CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE datandes_cms;

CREATE TABLE IF NOT EXISTS users (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(120) NOT NULL,
    email VARCHAR(180) NOT NULL UNIQUE,
    password_hash VARCHAR(255) NOT NULL,
    role ENUM('admin', 'editor') NOT NULL DEFAULT 'editor',
    twofa_secret VARCHAR(64) NULL,
    twofa_enabled TINYINT(1) NOT NULL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS sites (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(160) NOT NULL,
    slug VARCHAR(160) NOT NULL UNIQUE,
    domain_type ENUM('subdomain', 'custom') NOT NULL DEFAULT 'subdomain',
    custom_domain VARCHAR(190) NOT NULL DEFAULT '',
    subdomain VARCHAR(120) NOT NULL DEFAULT '',
    level TINYINT UNSIGNED NOT NULL,
    template_key VARCHAR(190) NOT NULL,
    business_name VARCHAR(190) NOT NULL,
    business_email VARCHAR(180) NOT NULL,
    business_phone VARCHAR(80) NOT NULL,
    business_address VARCHAR(220) NOT NULL,
    business_description TEXT NOT NULL,
    primary_color VARCHAR(16) NOT NULL DEFAULT '#1a2436',
    accent_color VARCHAR(16) NOT NULL DEFAULT '#b8963e',
    bg_color VARCHAR(16) NOT NULL DEFAULT '#f6f3ee',
    logo_url VARCHAR(255) NOT NULL DEFAULT '',
    hero_title VARCHAR(220) NOT NULL DEFAULT '',
    hero_subtitle TEXT NOT NULL,
    ftp_host VARCHAR(180) NOT NULL,
    ftp_port SMALLINT UNSIGNED NOT NULL DEFAULT 21,
    ftp_user VARCHAR(180) NOT NULL,
    ftp_pass_enc TEXT NOT NULL,
    ftp_path VARCHAR(220) NOT NULL DEFAULT '/public_html',
    ftp_passive TINYINT(1) NOT NULL DEFAULT 1,
    created_by INT UNSIGNED NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT NULL,
    INDEX idx_level (level),
    CONSTRAINT fk_sites_creator FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE RESTRICT
);

CREATE TABLE IF NOT EXISTS site_users (
    site_id INT UNSIGNED NOT NULL,
    user_id INT UNSIGNED NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (site_id, user_id),
    CONSTRAINT fk_site_users_site FOREIGN KEY (site_id) REFERENCES sites(id) ON DELETE CASCADE,
    CONSTRAINT fk_site_users_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS pages (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    site_id INT UNSIGNED NOT NULL,
    slug VARCHAR(80) NOT NULL,
    title VARCHAR(200) NOT NULL,
    seo_title VARCHAR(220) NOT NULL,
    meta_description VARCHAR(300) NOT NULL,
    content_html MEDIUMTEXT NOT NULL,
    position SMALLINT UNSIGNED NOT NULL DEFAULT 0,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uniq_site_slug (site_id, slug),
    CONSTRAINT fk_pages_site FOREIGN KEY (site_id) REFERENCES sites(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS posts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    site_id INT UNSIGNED NOT NULL,
    title VARCHAR(220) NOT NULL,
    slug VARCHAR(220) NOT NULL,
    excerpt VARCHAR(320) NOT NULL,
    content_html MEDIUMTEXT NOT NULL,
    status ENUM('draft', 'published') NOT NULL DEFAULT 'draft',
    published_at DATETIME NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uniq_site_post_slug (site_id, slug),
    CONSTRAINT fk_posts_site FOREIGN KEY (site_id) REFERENCES sites(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS contact_submissions (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    site_id INT UNSIGNED NOT NULL,
    name VARCHAR(160) NOT NULL,
    email VARCHAR(180) NOT NULL,
    phone VARCHAR(80) NOT NULL,
    message TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_contacts_site FOREIGN KEY (site_id) REFERENCES sites(id) ON DELETE CASCADE
);
