-- Migration: Update AI document monthly limit from 20 to 1000
-- Date: 2025-01-XX
-- Description: Updates the default monthly limit for AI document creation from 20 to 1000 to reflect the change in PPTX creation method (now counting AI message sends instead of document creations)

-- ===== Update Default Value in Companies Table =====
-- Update the default value for ai_document_monthly_limit column
ALTER TABLE companies 
MODIFY COLUMN ai_document_monthly_limit INT DEFAULT 1000 COMMENT '基本の月間利用制限（回数、会社単位、デフォルト値）';

-- ===== Update Default Value in AI Document Usage History Table =====
-- Update the default value for monthly_limit column
ALTER TABLE ai_document_usage_history
MODIFY COLUMN monthly_limit INT DEFAULT 1000 COMMENT 'その月の利用制限（回数、基本制限+増加分）';

-- ===== Update Existing Records =====
-- Update companies that still have the old default value (20) to 1000
UPDATE companies 
SET ai_document_monthly_limit = 1000 
WHERE ai_document_monthly_limit = 20 OR ai_document_monthly_limit IS NULL;

-- Update current month's history records that have the old default value (20) to 1000
-- Only update records for the current month to avoid changing historical data
UPDATE ai_document_usage_history
SET monthly_limit = 1000
WHERE monthly_limit = 20 
  AND year = YEAR(CURDATE())
  AND month = MONTH(CURDATE());

