<?php
declare(strict_types=1);

namespace App\Modules\HR\Models;

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

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

    protected static array $fillable = [
        'cycle_id', 'employee_profile_id', 'reviewer_id', 'review_date',
        'overall_rating', 'rating_label', 'scores_json',
        'strengths', 'improvements', 'goals_json',
        'employee_comment', 'status',
        'acknowledged_by_employee', 'acknowledged_at', 'notes',
    ];

    public static function getRatingLabels(): array
    {
        return [
            'excellent'  => 'ممتاز',
            'very_good'  => 'جيد جداً',
            'good'       => 'جيد',
            'acceptable' => 'مقبول',
            'poor'       => 'ضعيف',
        ];
    }

    public static function getStatuses(): array
    {
        return [
            'pending'       => 'في الانتظار',
            'in_progress'   => 'قيد التقييم',
            'submitted'     => 'تم التقديم',
            'acknowledged'  => 'تم الاطلاع',
        ];
    }

    public static function getRatingLabel(string $rating): string
    {
        $num = (float) $rating;
        if ($num >= 4.5) return 'excellent';
        if ($num >= 3.5) return 'very_good';
        if ($num >= 2.5) return 'good';
        if ($num >= 1.5) return 'acceptable';
        return 'poor';
    }

    public static function getForEmployee(int $employeeId): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT pr.*, pc.cycle_code, pc.name_ar as cycle_name, pc.year,
                    CONCAT(rev.first_name_ar, ' ', rev.last_name_ar) as reviewer_name
             FROM hr_performance_reviews pr
             JOIN hr_performance_cycles pc ON pc.id = pr.cycle_id
             LEFT JOIN hr_employee_profiles rev ON rev.employee_id = pr.reviewer_id
             WHERE pr.employee_profile_id = ?
             ORDER BY pc.year DESC",
            [$employeeId]
        );
    }
}
