DROP TABLE IF EXISTS purchases;
DROP TABLE IF EXISTS tickets;
DROP TABLE IF EXISTS sellers;
DROP TABLE IF EXISTS raffles;

CREATE TABLE sellers (
    id INTEGER PRIMARY KEY,
    username TEXT NOT NULL,
    password_hash TEXT NOT NULL
);

CREATE TABLE purchases (
    id TEXT PRIMARY KEY,
    seller_id INTEGER,
    total_numbers INTEGER,
    numbers_left INTEGER,
    remarks TEXT,
    FOREIGN KEY (seller_id) REFERENCES sellers (id)
);

CREATE TABLE raffles (
    id INTEGER PRIMARY KEY,
    description TEXT
);

CREATE TABLE tickets (
    id INTEGER PRIMARY KEY,
    purchase_id TEXT,
    raffle_id INTEGER,
    buyer_name TEXT,
    buyer_contact TEXT,
    chosen_number INTEGER,
    UNIQUE(raffle_id, chosen_number),
    FOREIGN KEY (purchase_id) REFERENCES purchases (id),
    FOREIGN KEY (raffle_id) REFERENCES raffles (id)
);