-- sql/migrations/staging/004_subscription_daily_counters.sql
-- Replace row-counting + soft-delete daily-quota model with plain
-- counters that reset at midnight (server local time / CURDATE()).
--
-- Two counters per subscription, one day field:
--   usage_day          = the date both counters currently represent
--   daily_reviews_used = on-demand log-review requests today
--   daily_explains_used = "Explain this to me" AI calls today
--
-- Rollover logic lives in the API:
--   UPDATE ... SET counter = IF(usage_day = CURDATE(), counter + 1, 1),
--                  usage_day = CURDATE()
--     WHERE id = ? AND (usage_day < CURDATE() OR counter < limit)
-- rowCount() == 0 means quota exceeded. Atomic, no soft-delete needed.

ALTER TABLE whp.ai_monitor_subscriptions
    ADD COLUMN IF NOT EXISTS usage_day           DATE NULL
        COMMENT 'Date the daily_*_used counters currently apply to (server TZ).',
    ADD COLUMN IF NOT EXISTS daily_reviews_used  INT NOT NULL DEFAULT 0
        COMMENT 'On-demand log reviews started today.',
    ADD COLUMN IF NOT EXISTS daily_explains_used INT NOT NULL DEFAULT 0
        COMMENT '"Explain this to me" AI calls today (cache hits do not count).';
