-- Create tasks table
CREATE TABLE `tasks` (
  `id` int NOT NULL AUTO_INCREMENT,
  `type` varchar(20) NOT NULL COMMENT 'referral, follow, other',
  `requirement` int NOT NULL COMMENT 'Number required (e.g., number of friends to invite)',
  `reward_amount` bigint NOT NULL COMMENT 'Reward amount in bigint format',
  `reward_type` varchar(20) NOT NULL DEFAULT 'Stars' COMMENT 'Stars, Power, etc.',
  `display_order` int NOT NULL DEFAULT 0 COMMENT 'Order for display',
  `title` varchar(255) NOT NULL COMMENT 'Task title (e.g., "Invite 1 friend")',
  `description` text COMMENT 'Task description',
  PRIMARY KEY (`id`),
  KEY `idx_type` (`type`),
  KEY `idx_display_order` (`display_order`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- Create user_task_claims table to track claimed tasks
-- If record exists, it means the task was claimed and reward was given
CREATE TABLE `user_task_claims` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `user_id` int NOT NULL,
  `task_id` int NOT NULL,
  `claimed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_user_task` (`user_id`, `task_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_task_id` (`task_id`),
  CONSTRAINT `fk_user_task_claims_user` FOREIGN KEY (`user_id`) REFERENCES `db_users_a` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_user_task_claims_task` FOREIGN KEY (`task_id`) REFERENCES `tasks` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- Insert default referral tasks
-- reward_amount is in bigint format (e.g., 2 Stars = 2000000, 5 Stars = 5000000)
INSERT INTO `tasks` (`type`, `requirement`, `reward_amount`, `reward_type`, `display_order`, `title`, `description`) VALUES
('referral', 1, 2000000, 'Stars', 1, 'Invite 1 friend', 'Invite 1 friend using your unique referral link'),
('referral', 3, 5000000, 'Stars', 2, 'Invite 3 friends', 'Invite 3 friends using your unique referral link'),
('referral', 7, 15000000, 'Stars', 3, 'Invite 7 friends', 'Invite 7 friends using your unique referral link'),
('referral', 15, 25000000, 'Stars', 4, 'Invite 15 friends', 'Invite 15 friends using your unique referral link'),
('referral', 30, 40000000, 'Stars', 5, 'Invite 30 friends', 'Invite 30 friends using your unique referral link'),
('referral', 50, 60000000, 'Stars', 6, 'Invite 50 friends', 'Invite 50 friends using your unique referral link'),
('referral', 100, 150000000, 'Stars', 7, 'Invite 100 friends', 'Invite 100 friends using your unique referral link');

