-- ============================================
-- HR 績效評核系統 - 資料庫結構
-- Database: db_A102
-- Version: 1.0
-- Created: 2025-12-03
-- ============================================

-- 使用資料庫
USE db_A102;

-- ============================================
-- 一、基礎資料表
-- ============================================

-- 1.1 組織架構表
CREATE TABLE IF NOT EXISTS hr_departments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    dept_code VARCHAR(20) UNIQUE NOT NULL COMMENT '部門代碼',
    dept_name_zh VARCHAR(100) NOT NULL COMMENT '部門名稱(中文)',
    dept_name_en VARCHAR(100) COMMENT '部門名稱(英文)',
    parent_dept_id INT COMMENT '上級部門ID',
    dept_level INT DEFAULT 1 COMMENT '部門層級',
    is_active BOOLEAN DEFAULT TRUE COMMENT '是否啟用',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_parent (parent_dept_id),
    INDEX idx_active (is_active),
    FOREIGN KEY (parent_dept_id) REFERENCES hr_departments(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='部門組織架構';

-- 1.2 職級表
CREATE TABLE IF NOT EXISTS hr_job_levels (
    id INT AUTO_INCREMENT PRIMARY KEY,
    level_code VARCHAR(20) UNIQUE NOT NULL COMMENT '職級代碼',
    level_name_zh VARCHAR(50) NOT NULL COMMENT '職級名稱(中文)',
    level_name_en VARCHAR(50) COMMENT '職級名稱(英文)',
    level_order INT NOT NULL COMMENT '職級順序(數字越大級別越高)',
    is_management BOOLEAN DEFAULT FALSE COMMENT '是否管理職',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_order (level_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='職級定義';

-- 1.3 人員表
CREATE TABLE IF NOT EXISTS hr_employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    employee_no VARCHAR(20) UNIQUE NOT NULL COMMENT '員工編號',
    email VARCHAR(100) UNIQUE NOT NULL COMMENT '電子郵件',
    name_zh VARCHAR(50) NOT NULL COMMENT '姓名(中文)',
    name_en VARCHAR(50) COMMENT '姓名(英文)',
    department_id INT NOT NULL COMMENT '部門ID',
    job_level_id INT NOT NULL COMMENT '職級ID',
    job_title_zh VARCHAR(100) COMMENT '職稱(中文)',
    job_title_en VARCHAR(100) COMMENT '職稱(英文)',
    supervisor_id INT COMMENT '直屬主管ID',
    hire_date DATE COMMENT '到職日期',
    status ENUM('active', 'inactive', 'resigned') DEFAULT 'active' COMMENT '員工狀態',
    preferred_language ENUM('zh-TW', 'en-US') DEFAULT 'zh-TW' COMMENT '偏好語言',
    password_hash VARCHAR(255) COMMENT '密碼雜湊',
    last_login_at TIMESTAMP NULL COMMENT '最後登入時間',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_dept (department_id),
    INDEX idx_supervisor (supervisor_id),
    INDEX idx_status (status),
    INDEX idx_email (email),
    FOREIGN KEY (department_id) REFERENCES hr_departments(id),
    FOREIGN KEY (job_level_id) REFERENCES hr_job_levels(id),
    FOREIGN KEY (supervisor_id) REFERENCES hr_employees(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='員工資料';

-- 1.4 系統角色表
CREATE TABLE IF NOT EXISTS hr_system_roles (
    id INT AUTO_INCREMENT PRIMARY KEY,
    role_code VARCHAR(20) UNIQUE NOT NULL COMMENT '角色代碼',
    role_name_zh VARCHAR(50) NOT NULL COMMENT '角色名稱(中文)',
    role_name_en VARCHAR(50) COMMENT '角色名稱(英文)',
    permissions JSON COMMENT '權限設定(JSON格式)',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系統角色權限';

-- 1.5 員工角色關聯表
CREATE TABLE IF NOT EXISTS hr_employee_roles (
    id INT AUTO_INCREMENT PRIMARY KEY,
    employee_id INT NOT NULL COMMENT '員工ID',
    role_id INT NOT NULL COMMENT '角色ID',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uk_employee_role (employee_id, role_id),
    FOREIGN KEY (employee_id) REFERENCES hr_employees(id) ON DELETE CASCADE,
    FOREIGN KEY (role_id) REFERENCES hr_system_roles(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='員工角色關聯';

-- ============================================
-- 二、職能字典
-- ============================================

-- 2.1 職能分類表
CREATE TABLE IF NOT EXISTS hr_competency_categories (
    id INT AUTO_INCREMENT PRIMARY KEY,
    category_code VARCHAR(20) UNIQUE NOT NULL COMMENT '分類代碼',
    category_name_zh VARCHAR(50) NOT NULL COMMENT '分類名稱(中文)',
    category_name_en VARCHAR(50) COMMENT '分類名稱(英文)',
    category_type ENUM('core', 'management', 'professional') NOT NULL COMMENT '職能類型',
    display_order INT DEFAULT 0 COMMENT '顯示順序',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_type (category_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='職能分類';

-- 2.2 職能字典表
CREATE TABLE IF NOT EXISTS hr_competency_dictionary (
    id INT AUTO_INCREMENT PRIMARY KEY,
    competency_code VARCHAR(30) UNIQUE NOT NULL COMMENT '職能代碼',
    category_id INT NOT NULL COMMENT '職能分類ID',
    competency_name_zh VARCHAR(100) NOT NULL COMMENT '職能名稱(中文)',
    competency_name_en VARCHAR(100) COMMENT '職能名稱(英文)',
    definition_zh TEXT NOT NULL COMMENT '職能定義(中文)',
    definition_en TEXT COMMENT '職能定義(英文)',
    is_active BOOLEAN DEFAULT TRUE COMMENT '是否啟用',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_category (category_id),
    INDEX idx_active (is_active),
    FOREIGN KEY (category_id) REFERENCES hr_competency_categories(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='職能字典';

-- 2.3 職能等級行為指標表
CREATE TABLE IF NOT EXISTS hr_competency_behaviors (
    id INT AUTO_INCREMENT PRIMARY KEY,
    competency_id INT NOT NULL COMMENT '職能ID',
    level_num INT NOT NULL COMMENT '等級(1-5)',
    behavior_description_zh TEXT NOT NULL COMMENT '行為指標(中文)',
    behavior_description_en TEXT COMMENT '行為指標(英文)',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_comp_level (competency_id, level_num),
    FOREIGN KEY (competency_id) REFERENCES hr_competency_dictionary(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='職能行為指標';

-- ============================================
-- 三、角色卡模組
-- ============================================

-- 3.1 角色卡主表
CREATE TABLE IF NOT EXISTS hr_role_cards (
    id INT AUTO_INCREMENT PRIMARY KEY,
    card_code VARCHAR(30) UNIQUE NOT NULL COMMENT '角色卡編號',
    card_type ENUM('template', 'personal') NOT NULL COMMENT '角色卡類型: template職位範本, personal個人角色卡',
    department_id INT NOT NULL COMMENT '部門ID',
    role_name_zh VARCHAR(100) NOT NULL COMMENT '角色名稱(中文)',
    role_name_en VARCHAR(100) COMMENT '角色名稱(英文)',
    job_level_id INT NOT NULL COMMENT '職級ID',
    employee_id INT COMMENT '綁定員工ID(個人角色卡)',
    mission_zh TEXT NOT NULL COMMENT '角色使命(中文)',
    mission_en TEXT COMMENT '角色使命(英文)',
    supervisor_id INT COMMENT '匯報對象ID',
    external_contacts TEXT COMMENT '外部接口',
    template_id INT COMMENT '來源範本ID(個人角色卡)',
    status ENUM('draft', 'pending_approval', 'approved', 'archived') DEFAULT 'draft' COMMENT '狀態',
    version INT DEFAULT 1 COMMENT '版本號',
    approved_by INT COMMENT '審批人ID',
    approved_at TIMESTAMP NULL COMMENT '審批時間',
    created_by INT NOT NULL COMMENT '建立人ID',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_type (card_type),
    INDEX idx_employee (employee_id),
    INDEX idx_status (status),
    INDEX idx_dept (department_id),
    FOREIGN KEY (department_id) REFERENCES hr_departments(id),
    FOREIGN KEY (job_level_id) REFERENCES hr_job_levels(id),
    FOREIGN KEY (employee_id) REFERENCES hr_employees(id) ON DELETE SET NULL,
    FOREIGN KEY (supervisor_id) REFERENCES hr_employees(id) ON DELETE SET NULL,
    FOREIGN KEY (template_id) REFERENCES hr_role_cards(id) ON DELETE SET NULL,
    FOREIGN KEY (approved_by) REFERENCES hr_employees(id),
    FOREIGN KEY (created_by) REFERENCES hr_employees(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='角色卡主表';

-- 3.2 角色卡核心職責表
CREATE TABLE IF NOT EXISTS hr_role_responsibilities (
    id INT AUTO_INCREMENT PRIMARY KEY,
    role_card_id INT NOT NULL COMMENT '角色卡ID',
    responsibility_zh TEXT NOT NULL COMMENT '職責描述(中文)',
    responsibility_en TEXT COMMENT '職責描述(英文)',
    display_order INT DEFAULT 0 COMMENT '顯示順序',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_role_card (role_card_id),
    FOREIGN KEY (role_card_id) REFERENCES hr_role_cards(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='角色卡職責';

-- 3.3 角色卡督導對象表
CREATE TABLE IF NOT EXISTS hr_role_supervisees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    role_card_id INT NOT NULL COMMENT '角色卡ID',
    supervisee_id INT NOT NULL COMMENT '督導對象ID(員工或角色)',
    supervisee_type ENUM('employee', 'role') NOT NULL COMMENT '督導對象類型',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_role_card (role_card_id),
    FOREIGN KEY (role_card_id) REFERENCES hr_role_cards(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='角色卡督導對象';

-- 3.4 角色卡協作夥伴表
CREATE TABLE IF NOT EXISTS hr_role_collaborators (
    id INT AUTO_INCREMENT PRIMARY KEY,
    role_card_id INT NOT NULL COMMENT '角色卡ID',
    collaborator_id INT NOT NULL COMMENT '協作對象ID',
    collaborator_type ENUM('employee', 'department', 'role') NOT NULL COMMENT '協作對象類型',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_role_card (role_card_id),
    FOREIGN KEY (role_card_id) REFERENCES hr_role_cards(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='角色卡協作夥伴';

-- 3.5 角色卡 KRA 表
CREATE TABLE IF NOT EXISTS hr_role_kra (
    id INT AUTO_INCREMENT PRIMARY KEY,
    role_card_id INT NOT NULL COMMENT '角色卡ID',
    kra_name_zh VARCHAR(200) NOT NULL COMMENT 'KRA名稱(中文)',
    kra_name_en VARCHAR(200) COMMENT 'KRA名稱(英文)',
    weight_percentage DECIMAL(5,2) NOT NULL COMMENT '權重百分比',
    display_order INT DEFAULT 0 COMMENT '顯示順序',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_role_card (role_card_id),
    FOREIGN KEY (role_card_id) REFERENCES hr_role_cards(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='角色卡KRA';

-- 3.6 角色卡 KPI 表
CREATE TABLE IF NOT EXISTS hr_role_kpi (
    id INT AUTO_INCREMENT PRIMARY KEY,
    kra_id INT NOT NULL COMMENT 'KRA ID',
    kpi_name_zh VARCHAR(200) NOT NULL COMMENT 'KPI指標名稱(中文)',
    kpi_name_en VARCHAR(200) COMMENT 'KPI指標名稱(英文)',
    measurement_zh TEXT NOT NULL COMMENT '衡量方式(中文)',
    measurement_en TEXT COMMENT '衡量方式(英文)',
    target_value VARCHAR(100) NOT NULL COMMENT '目標值',
    display_order INT DEFAULT 0 COMMENT '顯示順序',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_kra (kra_id),
    FOREIGN KEY (kra_id) REFERENCES hr_role_kra(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='角色卡KPI';

-- ============================================
-- 四、能力卡模組
-- ============================================

-- 4.1 能力卡主表
CREATE TABLE IF NOT EXISTS hr_competency_cards (
    id INT AUTO_INCREMENT PRIMARY KEY,
    card_code VARCHAR(30) UNIQUE NOT NULL COMMENT '能力卡編號',
    role_card_id INT NOT NULL COMMENT '關聯角色卡ID',
    employee_id INT NOT NULL COMMENT '員工ID',
    status ENUM('draft', 'active', 'archived') DEFAULT 'draft' COMMENT '狀態',
    version INT DEFAULT 1 COMMENT '版本號',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_role_card (role_card_id),
    INDEX idx_employee (employee_id),
    INDEX idx_status (status),
    FOREIGN KEY (role_card_id) REFERENCES hr_role_cards(id),
    FOREIGN KEY (employee_id) REFERENCES hr_employees(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='能力卡主表';

-- 4.2 能力卡硬性能力表
CREATE TABLE IF NOT EXISTS hr_competency_card_hard_skills (
    id INT AUTO_INCREMENT PRIMARY KEY,
    competency_card_id INT NOT NULL COMMENT '能力卡ID',
    skill_name_zh VARCHAR(100) NOT NULL COMMENT '技能名稱(中文)',
    skill_name_en VARCHAR(100) COMMENT '技能名稱(英文)',
    skill_category ENUM('knowledge', 'tool', 'certification') NOT NULL COMMENT '技能類別',
    is_required BOOLEAN DEFAULT TRUE COMMENT '是否必備',
    required_level INT NOT NULL COMMENT '要求熟練度(1-5)',
    description_zh TEXT COMMENT '說明(中文)',
    description_en TEXT COMMENT '說明(英文)',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_comp_card (competency_card_id),
    FOREIGN KEY (competency_card_id) REFERENCES hr_competency_cards(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='能力卡硬性能力';

-- 4.3 能力卡軟性能力表
CREATE TABLE IF NOT EXISTS hr_competency_card_soft_skills (
    id INT AUTO_INCREMENT PRIMARY KEY,
    competency_card_id INT NOT NULL COMMENT '能力卡ID',
    competency_dict_id INT NOT NULL COMMENT '職能字典ID',
    required_level INT NOT NULL COMMENT '要求等級(1-5)',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_comp_card (competency_card_id),
    INDEX idx_comp_dict (competency_dict_id),
    FOREIGN KEY (competency_card_id) REFERENCES hr_competency_cards(id) ON DELETE CASCADE,
    FOREIGN KEY (competency_dict_id) REFERENCES hr_competency_dictionary(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='能力卡軟性能力';

-- ============================================
-- 五、績效卡模組
-- ============================================

-- 5.1 評核週期設定表
CREATE TABLE IF NOT EXISTS hr_review_cycles (
    id INT AUTO_INCREMENT PRIMARY KEY,
    cycle_code VARCHAR(30) UNIQUE NOT NULL COMMENT '週期代碼(如: 2024-Q1, 2024-Annual)',
    cycle_year INT NOT NULL COMMENT '年度',
    cycle_type ENUM('quarterly', 'annual') NOT NULL COMMENT '週期類型',
    cycle_quarter INT COMMENT '季度(1-4)',
    start_date DATE NOT NULL COMMENT '開始日期',
    end_date DATE NOT NULL COMMENT '結束日期',
    review_deadline DATE COMMENT '評核截止日期',
    status ENUM('planned', 'active', 'closed') DEFAULT 'planned' COMMENT '狀態',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_year (cycle_year),
    INDEX idx_type (cycle_type),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='評核週期設定';

-- 5.2 績效卡主表
CREATE TABLE IF NOT EXISTS hr_performance_cards (
    id INT AUTO_INCREMENT PRIMARY KEY,
    card_code VARCHAR(30) UNIQUE NOT NULL COMMENT '績效卡編號',
    review_cycle_id INT NOT NULL COMMENT '評核週期ID',
    employee_id INT NOT NULL COMMENT '被評核人ID',
    role_card_id INT NOT NULL COMMENT '關聯角色卡ID',
    competency_card_id INT NOT NULL COMMENT '關聯能力卡ID',
    supervisor_id INT NOT NULL COMMENT '評核主管ID',
    self_review_date TIMESTAMP NULL COMMENT '自評完成時間',
    supervisor_review_date TIMESTAMP NULL COMMENT '主管評完成時間',
    discussion_date DATE COMMENT '績效面談日期',
    self_summary TEXT COMMENT '績效自評總結',
    supervisor_feedback TEXT COMMENT '主管建議回饋',
    goal_score DECIMAL(5,2) COMMENT '目標得分',
    behavior_score DECIMAL(5,2) COMMENT '行為得分',
    goal_weight DECIMAL(5,2) DEFAULT 70.00 COMMENT '目標佔比',
    behavior_weight DECIMAL(5,2) DEFAULT 30.00 COMMENT '行為佔比',
    total_score DECIMAL(5,2) COMMENT '總分',
    rating ENUM('A+', 'A', 'B+', 'B', 'C') COMMENT '考核等級',
    status ENUM('not_started', 'self_review', 'supervisor_review', 'completed', 'archived') DEFAULT 'not_started' COMMENT '狀態',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_cycle (review_cycle_id),
    INDEX idx_employee (employee_id),
    INDEX idx_supervisor (supervisor_id),
    INDEX idx_status (status),
    INDEX idx_rating (rating),
    FOREIGN KEY (review_cycle_id) REFERENCES hr_review_cycles(id),
    FOREIGN KEY (employee_id) REFERENCES hr_employees(id),
    FOREIGN KEY (role_card_id) REFERENCES hr_role_cards(id),
    FOREIGN KEY (competency_card_id) REFERENCES hr_competency_cards(id),
    FOREIGN KEY (supervisor_id) REFERENCES hr_employees(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='績效卡主表';

-- 5.3 績效卡目標評核表
CREATE TABLE IF NOT EXISTS hr_performance_goals (
    id INT AUTO_INCREMENT PRIMARY KEY,
    performance_card_id INT NOT NULL COMMENT '績效卡ID',
    kra_id INT NOT NULL COMMENT 'KRA ID',
    weight_percentage DECIMAL(5,2) NOT NULL COMMENT '權重百分比',
    actual_output TEXT COMMENT '具體產出描述',
    self_completion DECIMAL(5,2) COMMENT '自評完成度(%)',
    self_note TEXT COMMENT '自評說明',
    supervisor_completion DECIMAL(5,2) COMMENT '主管評完成度(%)',
    supervisor_note TEXT COMMENT '主管評說明',
    consensus_completion DECIMAL(5,2) COMMENT '共識完成度(%)',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_perf_card (performance_card_id),
    INDEX idx_kra (kra_id),
    FOREIGN KEY (performance_card_id) REFERENCES hr_performance_cards(id) ON DELETE CASCADE,
    FOREIGN KEY (kra_id) REFERENCES hr_role_kra(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='績效卡目標評核';

-- 5.4 績效卡行為評估表
CREATE TABLE IF NOT EXISTS hr_performance_behaviors (
    id INT AUTO_INCREMENT PRIMARY KEY,
    performance_card_id INT NOT NULL COMMENT '績效卡ID',
    competency_dict_id INT NOT NULL COMMENT '職能字典ID',
    self_level INT COMMENT '自評等級(1-5)',
    self_sbi_situation TEXT COMMENT '自評-情境(S)',
    self_sbi_behavior TEXT COMMENT '自評-行為(B)',
    self_sbi_impact TEXT COMMENT '自評-影響(I)',
    supervisor_level INT COMMENT '主管評等級(1-5)',
    supervisor_sbi_situation TEXT COMMENT '主管評-情境(S)',
    supervisor_sbi_behavior TEXT COMMENT '主管評-行為(B)',
    supervisor_sbi_impact TEXT COMMENT '主管評-影響(I)',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_perf_card (performance_card_id),
    INDEX idx_comp_dict (competency_dict_id),
    FOREIGN KEY (performance_card_id) REFERENCES hr_performance_cards(id) ON DELETE CASCADE,
    FOREIGN KEY (competency_dict_id) REFERENCES hr_competency_dictionary(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='績效卡行為評估';

-- ============================================
-- 六、成長卡模組
-- ============================================

-- 6.1 成長卡主表
CREATE TABLE IF NOT EXISTS hr_growth_cards (
    id INT AUTO_INCREMENT PRIMARY KEY,
    card_code VARCHAR(30) UNIQUE NOT NULL COMMENT '成長卡編號',
    performance_card_id INT NOT NULL COMMENT '關聯績效卡ID',
    employee_id INT NOT NULL COMMENT '員工ID',
    supervisor_id INT NOT NULL COMMENT '輔導主管ID',
    growth_period_start DATE NOT NULL COMMENT '成長週期開始',
    growth_period_end DATE NOT NULL COMMENT '成長週期結束',
    career_aspiration TEXT COMMENT '職業志向',
    employee_commitment TEXT COMMENT '員工承諾',
    employee_signed_at TIMESTAMP NULL COMMENT '員工簽署時間',
    supervisor_commitment TEXT COMMENT '主管承諾',
    supervisor_signed_at TIMESTAMP NULL COMMENT '主管簽署時間',
    review_frequency ENUM('monthly', 'quarterly') DEFAULT 'quarterly' COMMENT '檢視節奏',
    other_suggestions TEXT COMMENT '其他建議',
    status ENUM('draft', 'active', 'completed', 'archived') DEFAULT 'draft' COMMENT '狀態',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_perf_card (performance_card_id),
    INDEX idx_employee (employee_id),
    INDEX idx_supervisor (supervisor_id),
    INDEX idx_status (status),
    FOREIGN KEY (performance_card_id) REFERENCES hr_performance_cards(id),
    FOREIGN KEY (employee_id) REFERENCES hr_employees(id),
    FOREIGN KEY (supervisor_id) REFERENCES hr_employees(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='成長卡主表';

-- 6.2 成長卡發展焦點表
CREATE TABLE IF NOT EXISTS hr_growth_focus_areas (
    id INT AUTO_INCREMENT PRIMARY KEY,
    growth_card_id INT NOT NULL COMMENT '成長卡ID',
    focus_type ENUM('strength', 'development') NOT NULL COMMENT '焦點類型: strength優勢, development待發展',
    competency_dict_id INT COMMENT '關聯職能字典ID',
    kra_id INT COMMENT '關聯KRA ID',
    description TEXT COMMENT '描述',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_growth_card (growth_card_id),
    FOREIGN KEY (growth_card_id) REFERENCES hr_growth_cards(id) ON DELETE CASCADE,
    FOREIGN KEY (competency_dict_id) REFERENCES hr_competency_dictionary(id),
    FOREIGN KEY (kra_id) REFERENCES hr_role_kra(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='成長卡發展焦點';

-- 6.3 成長卡IDP計畫表
CREATE TABLE IF NOT EXISTS hr_growth_idp_goals (
    id INT AUTO_INCREMENT PRIMARY KEY,
    growth_card_id INT NOT NULL COMMENT '成長卡ID',
    goal_title TEXT NOT NULL COMMENT '發展目標(SMART)',
    competency_dict_id INT COMMENT '對應能力ID',
    timeline_start DATE COMMENT '開始時間',
    timeline_end DATE COMMENT '預計完成時間',
    success_criteria TEXT COMMENT '成果證據',
    status ENUM('pending', 'in_progress', 'completed', 'cancelled') DEFAULT 'pending' COMMENT '狀態',
    display_order INT DEFAULT 0 COMMENT '顯示順序',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_growth_card (growth_card_id),
    INDEX idx_comp_dict (competency_dict_id),
    FOREIGN KEY (growth_card_id) REFERENCES hr_growth_cards(id) ON DELETE CASCADE,
    FOREIGN KEY (competency_dict_id) REFERENCES hr_competency_dictionary(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='成長卡IDP目標';

-- 6.4 成長卡行動計畫表
CREATE TABLE IF NOT EXISTS hr_growth_actions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    idp_goal_id INT NOT NULL COMMENT 'IDP目標ID',
    action_description TEXT NOT NULL COMMENT '具體行動',
    resources_needed TEXT COMMENT '資源與支持',
    display_order INT DEFAULT 0 COMMENT '顯示順序',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_idp_goal (idp_goal_id),
    FOREIGN KEY (idp_goal_id) REFERENCES hr_growth_idp_goals(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='成長卡行動計畫';

-- 6.5 成長卡追蹤紀錄表
CREATE TABLE IF NOT EXISTS hr_growth_tracking (
    id INT AUTO_INCREMENT PRIMARY KEY,
    growth_card_id INT NOT NULL COMMENT '成長卡ID',
    review_date DATE NOT NULL COMMENT '檢視日期',
    progress_update TEXT NOT NULL COMMENT '進度更新',
    adjustments TEXT COMMENT '調整事項',
    employee_confirmed BOOLEAN DEFAULT FALSE COMMENT '員工確認',
    supervisor_confirmed BOOLEAN DEFAULT FALSE COMMENT '主管確認',
    confirmed_at TIMESTAMP NULL COMMENT '確認時間',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_growth_card (growth_card_id),
    INDEX idx_review_date (review_date),
    FOREIGN KEY (growth_card_id) REFERENCES hr_growth_cards(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='成長卡追蹤紀錄';

-- ============================================
-- 七、審批流程
-- ============================================

-- 7.1 審批記錄表
CREATE TABLE IF NOT EXISTS hr_approval_records (
    id INT AUTO_INCREMENT PRIMARY KEY,
    entity_type ENUM('role_card', 'competency_card', 'performance_card', 'growth_card') NOT NULL COMMENT '實體類型',
    entity_id INT NOT NULL COMMENT '實體ID',
    approver_id INT NOT NULL COMMENT '審批人ID',
    action ENUM('submit', 'approve', 'reject', 'return') NOT NULL COMMENT '操作',
    comments TEXT COMMENT '審批意見',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_entity (entity_type, entity_id),
    INDEX idx_approver (approver_id),
    FOREIGN KEY (approver_id) REFERENCES hr_employees(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='審批記錄';

-- ============================================
-- 八、系統設定
-- ============================================

-- 8.1 系統配置表
CREATE TABLE IF NOT EXISTS hr_system_settings (
    id INT AUTO_INCREMENT PRIMARY KEY,
    setting_key VARCHAR(50) UNIQUE NOT NULL COMMENT '設定鍵',
    setting_value TEXT COMMENT '設定值',
    setting_type ENUM('text', 'number', 'boolean', 'json') DEFAULT 'text' COMMENT '設定類型',
    description_zh VARCHAR(200) COMMENT '說明(中文)',
    description_en VARCHAR(200) COMMENT '說明(英文)',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    updated_by INT COMMENT '更新人ID',
    FOREIGN KEY (updated_by) REFERENCES hr_employees(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系統設定';

-- 8.2 等級分界設定表
CREATE TABLE IF NOT EXISTS hr_rating_thresholds (
    id INT AUTO_INCREMENT PRIMARY KEY,
    rating ENUM('A+', 'A', 'B+', 'B', 'C') NOT NULL UNIQUE COMMENT '等級',
    min_score DECIMAL(5,2) NOT NULL COMMENT '最低分數',
    max_score DECIMAL(5,2) NOT NULL COMMENT '最高分數',
    suggested_percentage DECIMAL(5,2) COMMENT '建議比例',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='等級分界設定';

-- ============================================
-- 九、版本管理
-- ============================================

-- 9.1 版本快照表
CREATE TABLE IF NOT EXISTS hr_version_snapshots (
    id INT AUTO_INCREMENT PRIMARY KEY,
    entity_type ENUM('role_card', 'competency_card', 'performance_card', 'growth_card') NOT NULL COMMENT '實體類型',
    entity_id INT NOT NULL COMMENT '實體ID',
    version_number INT NOT NULL COMMENT '版本號',
    snapshot_data JSON NOT NULL COMMENT '快照資料(JSON格式)',
    snapshot_reason VARCHAR(200) COMMENT '快照原因',
    created_by INT NOT NULL COMMENT '建立人ID',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_entity (entity_type, entity_id),
    INDEX idx_version (version_number),
    FOREIGN KEY (created_by) REFERENCES hr_employees(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='版本快照';

-- ============================================
-- 十、系統日誌
-- ============================================

-- 10.1 操作日誌表
CREATE TABLE IF NOT EXISTS hr_audit_logs (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id INT COMMENT '操作人ID',
    action VARCHAR(50) NOT NULL COMMENT '操作動作',
    entity_type VARCHAR(50) COMMENT '實體類型',
    entity_id INT COMMENT '實體ID',
    ip_address VARCHAR(45) COMMENT 'IP位址',
    user_agent TEXT COMMENT '使用者代理',
    request_data JSON COMMENT '請求資料',
    response_status INT COMMENT '回應狀態',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_user (user_id),
    INDEX idx_action (action),
    INDEX idx_entity (entity_type, entity_id),
    INDEX idx_created_at (created_at),
    FOREIGN KEY (user_id) REFERENCES hr_employees(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='操作日誌';

-- ============================================
-- 資料庫結構建立完成
-- ============================================
