-- =========================================================
-- StudyTrack :: Free-tier Limits Migration (Subjects + Music Search)
-- Run this once via phpMyAdmin (or `mysql -u ... -p db < database_v3_limits.sql`)
-- Safe to re-run — uses IF NOT EXISTS / INSERT IGNORE.
-- =========================================================

-- ── 1. New site_settings keys (admin-editable from Pro Subscriptions page) ──
INSERT IGNORE INTO `site_settings` (`setting_key`, `setting_value`) VALUES
  ('free_subject_limit',            '6'),
  ('free_music_search_daily_limit', '10');

-- ── 2. Daily music-search usage log (drives the free-tier search quota) ──
CREATE TABLE IF NOT EXISTS `music_search_log` (
  `id`         INT AUTO_INCREMENT PRIMARY KEY,
  `user_id`    INT NOT NULL,
  `query`      VARCHAR(150) DEFAULT NULL,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
  INDEX (`user_id`, `created_at`)
) ENGINE=InnoDB;
