-- Migration 168: Subscription scheduler and asset overlap indexes
-- Improves performance for due-subscription scans and asset overlap validation.

CREATE INDEX IF NOT EXISTS idx_sag_subscriptions_status_next_invoice_date
    ON sag_subscriptions(status, next_invoice_date);

CREATE INDEX IF NOT EXISTS idx_sag_subscription_items_asset_period
    ON sag_subscription_items(asset_id, period_from, period_to)
    WHERE asset_id IS NOT NULL;

CREATE INDEX IF NOT EXISTS idx_subscription_asset_bindings_asset_dates_active
    ON subscription_asset_bindings(asset_id, start_date, end_date)
    WHERE deleted_at IS NULL AND status = 'active';
