-- Migration: 017_fk_indexes
-- Add indexes on all foreign key columns that are not already covered by existing indexes.
-- Uses CREATE INDEX IF NOT EXISTS to be idempotent (safe to re-run).

-- =====================================================
-- auth / user tables
-- =====================================================

-- user_authtoken: user_id (unique FK, but ensure index exists)
CREATE UNIQUE INDEX IF NOT EXISTS idx_authtoken_user_id
    ON user_authtoken (user_id);

-- user_userprofile: user_id (unique FK)
CREATE UNIQUE INDEX IF NOT EXISTS idx_userprofile_user_id
    ON user_userprofile (user_id);

-- user_confirmationcode: user_id
CREATE INDEX IF NOT EXISTS idx_confirmationcode_user_id
    ON user_confirmationcode (user_id);

-- user_passwordresetcode: user_id
CREATE INDEX IF NOT EXISTS idx_passwordresetcode_user_id
    ON user_passwordresetcode (user_id);

-- user_applesocialauth: user_id (unique FK)
CREATE UNIQUE INDEX IF NOT EXISTS idx_applesocialauth_user_id
    ON user_applesocialauth (user_id);

-- user_googlesocialauth: user_id (unique FK)
CREATE UNIQUE INDEX IF NOT EXISTS idx_googlesocialauth_user_id
    ON user_googlesocialauth (user_id);

-- =====================================================
-- push notification device tables
-- =====================================================

-- push_notifications_apnsdevice: user_id
CREATE INDEX IF NOT EXISTS idx_apnsdevice_user_id
    ON push_notifications_apnsdevice (user_id);

-- push_notifications_gcmdevice: user_id
CREATE INDEX IF NOT EXISTS idx_gcmdevice_user_id
    ON push_notifications_gcmdevice (user_id);

-- =====================================================
-- notification tables
-- =====================================================

-- notifications_notificationpreference: user_id (unique FK)
CREATE UNIQUE INDEX IF NOT EXISTS idx_notificationpreference_user_id
    ON notifications_notificationpreference (user_id);

-- =====================================================
-- subscription tables
-- =====================================================

-- subscription_usersubscription: user_id (unique FK)
CREATE UNIQUE INDEX IF NOT EXISTS idx_subscription_user_id
    ON subscription_usersubscription (user_id);

-- =====================================================
-- residence tables
-- =====================================================

-- residence_residence: owner_id
CREATE INDEX IF NOT EXISTS idx_residence_owner_id
    ON residence_residence (owner_id);

-- residence_residencesharecode: residence_id (may already exist from model index tag via GORM)
CREATE INDEX IF NOT EXISTS idx_sharecode_residence_id
    ON residence_residencesharecode (residence_id);

-- residence_residencesharecode: created_by_id
CREATE INDEX IF NOT EXISTS idx_sharecode_created_by_id
    ON residence_residencesharecode (created_by_id);

-- =====================================================
-- task tables
-- =====================================================

-- task_task: created_by_id
CREATE INDEX IF NOT EXISTS idx_task_created_by_id
    ON task_task (created_by_id);

-- task_task: assigned_to_id
CREATE INDEX IF NOT EXISTS idx_task_assigned_to_id
    ON task_task (assigned_to_id);

-- task_task: category_id
CREATE INDEX IF NOT EXISTS idx_task_category_id
    ON task_task (category_id);

-- task_task: priority_id
CREATE INDEX IF NOT EXISTS idx_task_priority_id
    ON task_task (priority_id);

-- task_task: frequency_id
CREATE INDEX IF NOT EXISTS idx_task_frequency_id
    ON task_task (frequency_id);

-- task_task: contractor_id
CREATE INDEX IF NOT EXISTS idx_task_contractor_id
    ON task_task (contractor_id);

-- task_task: parent_task_id
CREATE INDEX IF NOT EXISTS idx_task_parent_task_id
    ON task_task (parent_task_id);

-- task_taskcompletionimage: completion_id
CREATE INDEX IF NOT EXISTS idx_completionimage_completion_id
    ON task_taskcompletionimage (completion_id);

-- task_document: created_by_id
CREATE INDEX IF NOT EXISTS idx_document_created_by_id
    ON task_document (created_by_id);

-- task_document: task_id
CREATE INDEX IF NOT EXISTS idx_document_task_id
    ON task_document (task_id);

-- task_documentimage: document_id
CREATE INDEX IF NOT EXISTS idx_documentimage_document_id
    ON task_documentimage (document_id);

-- task_contractor: residence_id
CREATE INDEX IF NOT EXISTS idx_contractor_residence_id
    ON task_contractor (residence_id);

-- task_reminderlog: notification_id
CREATE INDEX IF NOT EXISTS idx_reminderlog_notification_id
    ON task_reminderlog (notification_id);
