-- Mail List Manager Database Schema

-- Table: lists
-- Stores mailing list information
CREATE TABLE IF NOT EXISTS lists (
    list_id INT AUTO_INCREMENT PRIMARY KEY,
    list_name VARCHAR(100) NOT NULL UNIQUE,
    list_email VARCHAR(255) NOT NULL UNIQUE,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    active BOOLEAN DEFAULT TRUE,
    INDEX idx_list_email (list_email),
    INDEX idx_active (active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Table: members
-- Stores member information
CREATE TABLE IF NOT EXISTS members (
    member_id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL UNIQUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    active BOOLEAN DEFAULT TRUE,
    INDEX idx_email (email),
    INDEX idx_active (active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Table: list_members
-- Junction table for many-to-many relationship between lists and members
CREATE TABLE IF NOT EXISTS list_members (
    id INT AUTO_INCREMENT PRIMARY KEY,
    list_id INT NOT NULL,
    member_id INT NOT NULL,
    subscribed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    active BOOLEAN DEFAULT TRUE,
    FOREIGN KEY (list_id) REFERENCES lists(list_id) ON DELETE CASCADE,
    FOREIGN KEY (member_id) REFERENCES members(member_id) ON DELETE CASCADE,
    UNIQUE KEY unique_list_member (list_id, member_id),
    INDEX idx_list_id (list_id),
    INDEX idx_member_id (member_id),
    INDEX idx_active (active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Insert sample data
INSERT INTO lists (list_name, list_email, description) VALUES
    ('Community', 'community@lists.sasalliance.org', 'General community announcements'),
    ('Board', 'board@lists.sasalliance.org', 'Board members only'),
    ('Members', 'members@lists.sasalliance.org', 'All members'),
    ('Announcements', 'announcements@lists.sasalliance.org', 'Important announcements');

INSERT INTO members (name, email) VALUES
    ('James Pattinson', 'james.pattinson@sasalliance.org'),
    ('James Pattinson (Personal)', 'james@pattinson.org');

-- Subscribe members to lists
-- Community list - both addresses
INSERT INTO list_members (list_id, member_id) VALUES
    (1, 1), -- James (work) on Community
    (1, 2); -- James (personal) on Community

-- Board list - work address only
INSERT INTO list_members (list_id, member_id) VALUES
    (2, 1); -- James (work) on Board

-- Members list - both addresses
INSERT INTO list_members (list_id, member_id) VALUES
    (3, 1), -- James (work) on Members
    (3, 2); -- James (personal) on Members

-- Announcements list - both addresses
INSERT INTO list_members (list_id, member_id) VALUES
    (4, 1), -- James (work) on Announcements
    (4, 2); -- James (personal) on Announcements
