CREATE TABLE IF NOT EXISTS files (
    id             TEXT PRIMARY KEY,
    filename       TEXT NOT NULL,
    r2_key         TEXT NOT NULL UNIQUE,
    size_bytes     INTEGER NOT NULL,
    content_type   TEXT NOT NULL,
    uploaded_at    DATETIME DEFAULT (datetime('now')),
    expires_at     DATETIME,
    password_hash  TEXT,
    delete_token   TEXT NOT NULL UNIQUE,
    download_count INTEGER DEFAULT 0,
    batch_id       TEXT
);

CREATE INDEX IF NOT EXISTS idx_files_expires_at ON files(expires_at) WHERE expires_at IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_files_delete_token ON files(delete_token);
CREATE INDEX IF NOT EXISTS idx_files_batch_id ON files(batch_id) WHERE batch_id IS NOT NULL;
