-- 竞赛报名表
CREATE TABLE IF NOT EXISTS `t_biz_contest_registration` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键 ID',
    `contest_id` BIGINT NOT NULL COMMENT '赛事 ID',
    `tenant_id` BIGINT NOT NULL COMMENT '所属租户 ID',
    `registration_type` VARCHAR(50) DEFAULT NULL COMMENT '报名类型：individual/team',
    `team_id` BIGINT DEFAULT NULL COMMENT '团队 ID',
    `team_name` VARCHAR(100) DEFAULT NULL COMMENT '团队名称快照（团队赛）',
    `user_id` BIGINT NOT NULL COMMENT '账号 ID',
    `account_no` VARCHAR(50) NOT NULL COMMENT '报名账号',
    `account_name` VARCHAR(100) NOT NULL COMMENT '报名账号名称',
    `role` VARCHAR(50) DEFAULT NULL COMMENT '报名角色快照：leader/member/mentor',
    `registration_state` VARCHAR(50) DEFAULT 'pending' COMMENT '报名状态：pending/passed/rejected/withdrawn',
    `participant_type` VARCHAR(50) DEFAULT 'self' COMMENT '参与者类型：self-自己/child-代子女报名',
    `child_id` BIGINT DEFAULT NULL COMMENT '子女 ID',
    `registrant` BIGINT DEFAULT NULL COMMENT '实际报名人用户 ID',
    `registration_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '报名时间',
    `reason` VARCHAR(1023) DEFAULT NULL COMMENT '审核理由',
    `operator` BIGINT DEFAULT NULL COMMENT '审核人用户 ID',
    `operation_date` DATETIME DEFAULT NULL COMMENT '审核时间',
    `create_by` VARCHAR(50) DEFAULT NULL COMMENT '创建人',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_by` VARCHAR(50) DEFAULT NULL COMMENT '更新人',
    `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `deleted` TINYINT DEFAULT 0 COMMENT '逻辑删除标识（0-未删除，1-已删除）',
    PRIMARY KEY (`id`),
    KEY `idx_contest_id` (`contest_id`),
    KEY `idx_tenant_id` (`tenant_id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_team_id` (`team_id`),
    KEY `idx_registration_state` (`registration_state`),
    KEY `idx_registration_time` (`registration_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='竞赛报名表';

-- 竞赛报名指导老师关联表
CREATE TABLE IF NOT EXISTS `t_biz_contest_registration_teacher` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键 ID',
    `registration_id` BIGINT NOT NULL COMMENT '报名 ID',
    `user_id` BIGINT NOT NULL COMMENT '老师用户 ID',
    `tenant_id` BIGINT NOT NULL COMMENT '租户 ID',
    `is_default` TINYINT DEFAULT 0 COMMENT '是否默认指导老师',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_registration_user` (`registration_id`, `user_id`),
    KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='竞赛报名指导老师关联表';

-- 竞赛团队表
CREATE TABLE IF NOT EXISTS `t_biz_contest_team` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键 ID',
    `tenant_id` BIGINT NOT NULL COMMENT '团队所属租户 ID',
    `contest_id` BIGINT NOT NULL COMMENT '赛事 ID',
    `team_name` VARCHAR(100) NOT NULL COMMENT '团队名称',
    `leader_user_id` BIGINT NOT NULL COMMENT '团队负责人用户 ID',
    `max_members` INT DEFAULT NULL COMMENT '团队最大成员数',
    `create_by` VARCHAR(50) DEFAULT NULL COMMENT '创建人',
    `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_by` VARCHAR(50) DEFAULT NULL COMMENT '更新人',
    `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `valid_state` TINYINT DEFAULT 1 COMMENT '有效状态：1-有效，2-失效',
    `deleted` TINYINT DEFAULT 0 COMMENT '逻辑删除标识（0-未删除，1-已删除）',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_tenant_contest_team` (`tenant_id`, `contest_id`, `team_name`),
    KEY `idx_contest_id` (`contest_id`),
    KEY `idx_leader_user_id` (`leader_user_id`),
    KEY `idx_valid_state` (`valid_state`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='竞赛团队表';

-- 竞赛团队成员表
CREATE TABLE IF NOT EXISTS `t_biz_contest_team_member` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键 ID',
    `tenant_id` BIGINT NOT NULL COMMENT '租户 ID',
    `team_id` BIGINT NOT NULL COMMENT '团队 ID',
    `contest_id` BIGINT NOT NULL COMMENT '赛事 ID',
    `user_id` BIGINT NOT NULL COMMENT '用户 ID',
    `role` VARCHAR(50) DEFAULT 'member' COMMENT '团队角色：leader/member/mentor',
    `join_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '加入时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_team_user` (`team_id`, `user_id`),
    KEY `idx_tenant_id` (`tenant_id`),
    KEY `idx_contest_id` (`contest_id`),
    KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='竞赛团队成员表';
