<?php
declare(strict_types=1);

return [
    'up' => "
CREATE TABLE IF NOT EXISTS hr_overtime_types (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name_ar VARCHAR(100) NOT NULL,
    name_en VARCHAR(100) NULL,
    rate_multiplier DECIMAL(4,2) NOT NULL DEFAULT 1.50,
    max_hours_per_day DECIMAL(4,2) NULL,
    max_hours_per_month DECIMAL(5,2) NULL,
    requires_approval TINYINT(1) NOT NULL DEFAULT 1,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS hr_overtime_requests (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    employee_id INT UNSIGNED NOT NULL,
    overtime_type_id INT UNSIGNED NOT NULL,
    request_date DATE NOT NULL,
    start_time TIME NOT NULL,
    end_time TIME NOT NULL,
    hours DECIMAL(4,2) NOT NULL,
    reason VARCHAR(500) NULL,
    status ENUM('pending','approved','rejected','cancelled') NOT NULL DEFAULT 'pending',
    approved_by INT UNSIGNED NULL,
    approved_at DATETIME NULL,
    rejection_reason VARCHAR(500) NULL,
    payroll_run_id INT UNSIGNED NULL,
    calculated_amount DECIMAL(12,2) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_employee (employee_id),
    KEY idx_date (request_date),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS hr_attendance_violations (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    employee_id INT UNSIGNED NOT NULL,
    attendance_id INT UNSIGNED NULL,
    violation_date DATE NOT NULL,
    violation_type ENUM('late_arrival','early_departure','absence','unauthorized_leave','missing_punch') NOT NULL,
    scheduled_time TIME NULL,
    actual_time TIME NULL,
    difference_minutes INT NOT NULL DEFAULT 0,
    is_excused TINYINT(1) NOT NULL DEFAULT 0,
    excuse_reason VARCHAR(500) NULL,
    penalty_applied TINYINT(1) NOT NULL DEFAULT 0,
    penalty_amount DECIMAL(12,2) NULL,
    penalty_description VARCHAR(255) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_employee (employee_id),
    KEY idx_date (violation_date),
    KEY idx_type (violation_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS hr_permission_requests (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    employee_id INT UNSIGNED NOT NULL,
    permission_date DATE NOT NULL,
    start_time TIME NOT NULL,
    end_time TIME NOT NULL,
    hours DECIMAL(4,2) NOT NULL,
    reason VARCHAR(500) NOT NULL,
    status ENUM('pending','approved','rejected') NOT NULL DEFAULT 'pending',
    approved_by INT UNSIGNED NULL,
    approved_at DATETIME NULL,
    monthly_total_hours DECIMAL(5,2) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_employee (employee_id),
    KEY idx_date (permission_date),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS hr_shifts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name_ar VARCHAR(100) NOT NULL,
    name_en VARCHAR(100) NULL,
    start_time TIME NOT NULL,
    end_time TIME NOT NULL,
    break_start TIME NULL,
    break_end TIME NULL,
    break_duration_minutes INT UNSIGNED NOT NULL DEFAULT 0,
    working_hours DECIMAL(4,2) NOT NULL,
    grace_period_minutes INT UNSIGNED NOT NULL DEFAULT 15,
    early_leave_threshold_minutes INT UNSIGNED NOT NULL DEFAULT 15,
    is_night_shift TINYINT(1) NOT NULL DEFAULT 0,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS hr_shift_assignments (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    employee_id INT UNSIGNED NOT NULL,
    shift_id INT UNSIGNED NOT NULL,
    start_date DATE NOT NULL,
    end_date DATE NULL,
    rotation_pattern ENUM('fixed','weekly','biweekly','monthly') NOT NULL DEFAULT 'fixed',
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_employee (employee_id),
    KEY idx_shift (shift_id),
    KEY idx_active (is_active, start_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS hr_insurance_config (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    effective_date DATE NOT NULL,
    basic_salary_cap DECIMAL(12,2) NOT NULL,
    variable_salary_cap DECIMAL(12,2) NOT NULL,
    employer_basic_rate DECIMAL(5,4) NOT NULL,
    employer_variable_rate DECIMAL(5,4) NOT NULL,
    employee_basic_rate DECIMAL(5,4) NOT NULL,
    employee_variable_rate DECIMAL(5,4) NOT NULL,
    min_subscription_salary DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_effective (effective_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS hr_tax_brackets (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    bracket_order INT UNSIGNED NOT NULL,
    from_amount DECIMAL(15,2) NOT NULL,
    to_amount DECIMAL(15,2) NOT NULL,
    rate DECIMAL(5,4) NOT NULL,
    annual_exemption DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    effective_date DATE NOT NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_effective (effective_date),
    KEY idx_order (bracket_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
    'down' => "
DROP TABLE IF EXISTS hr_tax_brackets;
DROP TABLE IF EXISTS hr_insurance_config;
DROP TABLE IF EXISTS hr_shift_assignments;
DROP TABLE IF EXISTS hr_shifts;
DROP TABLE IF EXISTS hr_permission_requests;
DROP TABLE IF EXISTS hr_attendance_violations;
DROP TABLE IF EXISTS hr_overtime_requests;
DROP TABLE IF EXISTS hr_overtime_types",
];
