CREATE TABLE IF NOT EXISTS admins (id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,email VARCHAR(255) NOT NULL UNIQUE,password_hash VARCHAR(255) NOT NULL);
CREATE TABLE IF NOT EXISTS contacts (id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,email VARCHAR(255) NOT NULL UNIQUE,first_name VARCHAR(120) NOT NULL DEFAULT '',status ENUM('active','unsubscribed') NOT NULL DEFAULT 'active',created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,unsubscribed_at TIMESTAMP NULL);
CREATE TABLE IF NOT EXISTS campaigns (id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,subject VARCHAR(255) NOT NULL,html MEDIUMTEXT NOT NULL,status ENUM('draft','queued','completed') NOT NULL DEFAULT 'draft',created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
CREATE TABLE IF NOT EXISTS deliveries (id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,campaign_id BIGINT UNSIGNED NOT NULL,contact_id BIGINT UNSIGNED NOT NULL,status ENUM('pending','sent','failed','skipped') NOT NULL DEFAULT 'pending',attempts INT NOT NULL DEFAULT 0,error_message VARCHAR(500) NULL,sent_at TIMESTAMP NULL,UNIQUE KEY uniq_delivery(campaign_id,contact_id),INDEX idx_status(status),FOREIGN KEY(campaign_id) REFERENCES campaigns(id),FOREIGN KEY(contact_id) REFERENCES contacts(id));
