-- +goose Up
-- Add Gmail-specific fields to emails table
ALTER TABLE emails 
    ADD COLUMN gmail_message_id VARCHAR(255) UNIQUE AFTER id,
    ADD COLUMN gmail_thread_id VARCHAR(255) AFTER gmail_message_id,
    ADD COLUMN is_downloaded BOOLEAN DEFAULT FALSE AFTER email_attachment_count,
    ADD COLUMN raw_headers TEXT AFTER subject;

-- +goose StatementBegin
CREATE INDEX idx_gmail_message_id ON emails(gmail_message_id);
-- +goose StatementEnd

-- +goose StatementBegin
CREATE INDEX idx_gmail_thread_id ON emails(gmail_thread_id);
-- +goose StatementEnd

-- Add Gmail-specific fields to email_attachments
ALTER TABLE email_attachments
    ADD COLUMN gmail_attachment_id VARCHAR(255) AFTER email_id,
    ADD COLUMN gmail_message_id VARCHAR(255) AFTER gmail_attachment_id,
    ADD COLUMN content_id VARCHAR(255) AFTER filename;

-- +goose StatementBegin
CREATE INDEX idx_gmail_attachment_id ON email_attachments(gmail_attachment_id);
-- +goose StatementEnd

-- +goose Down
-- Remove indexes
-- +goose StatementBegin
DROP INDEX idx_gmail_attachment_id ON email_attachments;
-- +goose StatementEnd

-- +goose StatementBegin
DROP INDEX idx_gmail_thread_id ON emails;
-- +goose StatementEnd

-- +goose StatementBegin
DROP INDEX idx_gmail_message_id ON emails;
-- +goose StatementEnd

-- Remove columns from email_attachments
ALTER TABLE email_attachments
    DROP COLUMN content_id,
    DROP COLUMN gmail_message_id,
    DROP COLUMN gmail_attachment_id;

-- Remove columns from emails
ALTER TABLE emails
    DROP COLUMN raw_headers,
    DROP COLUMN is_downloaded,
    DROP COLUMN gmail_thread_id,
    DROP COLUMN gmail_message_id;