-- Email Marketing Module Database Schema
-- Run this script to set up the database

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

-- Lists table
CREATE TABLE IF NOT EXISTS lists (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;

-- List memberships
CREATE TABLE IF NOT EXISTS list_subscribers (
    list_id INT NOT NULL,
    subscriber_id INT NOT NULL,
    added_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (list_id, subscriber_id),
    FOREIGN KEY (list_id) REFERENCES lists(id) ON DELETE CASCADE,
    FOREIGN KEY (subscriber_id) REFERENCES subscribers(id) ON DELETE CASCADE
) ENGINE=InnoDB;

-- Campaigns table
CREATE TABLE IF NOT EXISTS campaigns (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(200) NOT NULL,
    subject VARCHAR(300) NOT NULL,
    preview_text VARCHAR(200),
    from_name VARCHAR(100) NOT NULL,
    from_email VARCHAR(255) NOT NULL,
    reply_to VARCHAR(255),
    html_content LONGTEXT,
    text_content LONGTEXT,
    status ENUM('draft', 'scheduled', 'sending', 'sent', 'paused') DEFAULT 'draft',
    campaign_type ENUM('newsletter', 'promotional', 'announcement', 'welcome') DEFAULT 'newsletter',
    scheduled_at DATETIME NULL,
    sent_at DATETIME NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    total_recipients INT DEFAULT 0,
    sent_count INT DEFAULT 0,
    INDEX idx_status (status)
) ENGINE=InnoDB;

-- Campaign lists (which lists a campaign was sent to)
CREATE TABLE IF NOT EXISTS campaign_lists (
    campaign_id INT NOT NULL,
    list_id INT NOT NULL,
    PRIMARY KEY (campaign_id, list_id),
    FOREIGN KEY (campaign_id) REFERENCES campaigns(id) ON DELETE CASCADE,
    FOREIGN KEY (list_id) REFERENCES lists(id) ON DELETE CASCADE
) ENGINE=InnoDB;

-- Email sends / tracking
CREATE TABLE IF NOT EXISTS email_sends (
    id INT AUTO_INCREMENT PRIMARY KEY,
    campaign_id INT NOT NULL,
    subscriber_id INT NOT NULL,
    status ENUM('queued', 'sent', 'failed', 'bounced') DEFAULT 'queued',
    sent_at DATETIME NULL,
    opened_at DATETIME NULL,
    open_count INT DEFAULT 0,
    click_count INT DEFAULT 0,
    tracking_pixel VARCHAR(64),
    FOREIGN KEY (campaign_id) REFERENCES campaigns(id) ON DELETE CASCADE,
    FOREIGN KEY (subscriber_id) REFERENCES subscribers(id) ON DELETE CASCADE,
    INDEX idx_campaign (campaign_id),
    INDEX idx_subscriber (subscriber_id),
    INDEX idx_tracking (tracking_pixel)
) ENGINE=InnoDB;

-- Email templates
CREATE TABLE IF NOT EXISTS templates (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    description VARCHAR(300),
    thumbnail VARCHAR(255),
    html_content LONGTEXT,
    category ENUM('newsletter', 'promotional', 'announcement', 'welcome', 'custom') DEFAULT 'newsletter',
    is_default TINYINT(1) DEFAULT 0,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

-- Settings table
CREATE TABLE IF NOT EXISTS settings (
    setting_key VARCHAR(100) PRIMARY KEY,
    setting_value TEXT,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;

-- Insert default settings
INSERT INTO settings (setting_key, setting_value) VALUES
('company_name', 'Your Company'),
('company_logo_url', 'assets/images/logo.png'),
('company_website', 'https://yourcompany.com'),
('company_address', '123 Business Street, City, Country'),
('from_name', 'Your Company Newsletter'),
('from_email', 'newsletter@yourcompany.com'),
('reply_to', 'support@yourcompany.com'),
('unsubscribe_base_url', 'https://yourcompany.com/email-marketing/public/'),
('verify_base_url', 'https://yourcompany.com/email-marketing/public/'),
('brand_color', '#2563EB'),
('brand_accent', '#F59E0B')
ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value);

-- Insert default list
INSERT INTO lists (name, description) VALUES
('Main Newsletter', 'Primary newsletter subscriber list'),
('Promotions', 'Promotional offers and deals'),
('Announcements', 'Company announcements and updates')
ON DUPLICATE KEY UPDATE name = name;

-- Insert a sample template
INSERT INTO templates (name, description, category, is_default, html_content) VALUES
('Default Newsletter', 'Clean newsletter template with header and footer', 'newsletter', 1, '')
ON DUPLICATE KEY UPDATE name = name;
