<?php
declare(strict_types=1);

namespace App\Modules\HR\Models;

use App\Core\Model;
use App\Core\App;
use App\Core\Pagination;

class HrSalaryAdjustment extends Model
{
    protected static string $table = 'hr_salary_adjustments';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = true;
    protected static bool $softDelete = false;
    protected static bool $autoTrackAuthor = true;

    protected static array $fillable = [
        'employee_profile_id', 'adjustment_type', 'previous_basic_salary', 'new_basic_salary',
        'previous_job_title_id', 'new_job_title_id', 'effective_date',
        'reason', 'approved_by', 'approved_at', 'status', 'notes',
    ];

    public static function getAdjustmentTypes(): array
    {
        return [
            'raise'             => 'زيادة راتب',
            'promotion'         => 'ترقية',
            'demotion'          => 'تخفيض درجة',
            'annual_increment'  => 'علاوة سنوية',
            'correction'        => 'تصحيح',
        ];
    }

    public static function getStatuses(): array
    {
        return [
            'pending'  => 'في الانتظار',
            'approved' => 'معتمد',
            'applied'  => 'تم التطبيق',
            'rejected' => 'مرفوض',
        ];
    }

    public static function getForEmployee(int $employeeId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT sa.*, jold.name_ar as old_job_title, jnew.name_ar as new_job_title
             FROM hr_salary_adjustments sa
             LEFT JOIN hr_job_titles jold ON jold.id = sa.previous_job_title_id
             LEFT JOIN hr_job_titles jnew ON jnew.id = sa.new_job_title_id
             WHERE sa.employee_profile_id = ?
             ORDER BY sa.effective_date DESC",
            [$employeeId]
        );
    }

    public static function search(array $filters, int $perPage = 25, int $page = 1): array
    {
        $db = App::getInstance()->db();
        $where = '1=1';
        $params = [];

        if (!empty($filters['status'])) {
            $where .= ' AND sa.status = ?';
            $params[] = $filters['status'];
        }
        if (!empty($filters['adjustment_type'])) {
            $where .= ' AND sa.adjustment_type = ?';
            $params[] = $filters['adjustment_type'];
        }

        $countRow = $db->selectOne(
            "SELECT COUNT(*) as cnt FROM hr_salary_adjustments sa
             JOIN hr_employee_profiles p ON p.id = sa.employee_profile_id WHERE {$where}",
            $params
        );
        $total = (int) ($countRow['cnt'] ?? 0);
        $offset = ($page - 1) * $perPage;

        $rows = $db->select(
            "SELECT sa.*, CONCAT(p.first_name_ar, ' ', p.last_name_ar) as full_name_ar, p.employee_number
             FROM hr_salary_adjustments sa
             JOIN hr_employee_profiles p ON p.id = sa.employee_profile_id
             WHERE {$where}
             ORDER BY sa.effective_date DESC
             LIMIT {$perPage} OFFSET {$offset}",
            $params
        );

        return ['data' => $rows, 'pagination' => Pagination::paginate($total, $perPage, $page)];
    }
}
