BEGIN;

-- Add index for users.email for faster email lookups
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);

-- Add indexes for user_department relationship (singular table name)
CREATE INDEX IF NOT EXISTS idx_user_department_user_id ON user_department(user_id);
CREATE INDEX IF NOT EXISTS idx_user_department_department_id ON user_department(department_id);

-- Add indexes for letter_outgoing_recipients for faster joins
CREATE INDEX IF NOT EXISTS idx_letter_outgoing_recipients_user_id ON letter_outgoing_recipients(user_id);
CREATE INDEX IF NOT EXISTS idx_letter_outgoing_recipients_letter_id ON letter_outgoing_recipients(letter_id);

-- Add index for letters_outgoing status queries
CREATE INDEX IF NOT EXISTS idx_letters_outgoing_status ON letters_outgoing(status) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_letters_outgoing_deleted_at ON letters_outgoing(deleted_at);

-- Add index for letters_incoming status queries
CREATE INDEX IF NOT EXISTS idx_letters_incoming_status ON letters_incoming(status) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_letters_incoming_deleted_at ON letters_incoming(deleted_at);

-- Add index for letter_incoming_recipients for department lookups
CREATE INDEX IF NOT EXISTS idx_letter_incoming_recipients_dept_id ON letter_incoming_recipients(recipient_department_id);
CREATE INDEX IF NOT EXISTS idx_letter_incoming_recipients_user_id ON letter_incoming_recipients(recipient_user_id);

COMMIT;