<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AttendanceCorrection extends Model
{
    use HasFactory, HasUuids;

    protected $fillable = [
        'user_id',
        'company_id',
        'attendance_id',
        'target_date',
        'correction_type',
        'original_check_in',
        'original_check_out',
        'original_duration',
        'corrected_check_in',
        'corrected_check_out',
        'corrected_duration',
        'reason',
        'status',
        'approved_by',
        'approved_at',
        'rejection_reason',
        'simulated_request_at',
        'is_auto_approved',
    ];

    protected $casts = [
        'user_id' => 'integer',
        'company_id' => 'integer',
        'approved_by' => 'integer',
        'target_date' => 'date',
        'original_check_in' => 'datetime:H:i',
        'original_check_out' => 'datetime:H:i',
        'corrected_check_in' => 'datetime:H:i',
        'corrected_check_out' => 'datetime:H:i',
        'approved_at' => 'datetime',
        'simulated_request_at' => 'datetime',
        'is_auto_approved' => 'boolean',
    ];

    // Correction types
    public const TYPE_CHECK_IN = 'check_in';
    public const TYPE_CHECK_OUT = 'check_out';
    public const TYPE_BOTH = 'both';
    public const TYPE_DURATION = 'duration';

    public const CORRECTION_TYPES = [
        self::TYPE_CHECK_IN => 'Koreksi Check In',
        self::TYPE_CHECK_OUT => 'Koreksi Check Out',
        self::TYPE_BOTH => 'Koreksi Check In & Out',
        self::TYPE_DURATION => 'Koreksi Durasi Kerja',
    ];

    // Status constants
    public const STATUS_PENDING = 'pending';
    public const STATUS_APPROVED = 'approved';
    public const STATUS_REJECTED = 'rejected';
    public const STATUS_AUTO_APPROVED = 'auto_approved';

    public const STATUSES = [
        self::STATUS_PENDING => 'Menunggu',
        self::STATUS_APPROVED => 'Disetujui',
        self::STATUS_REJECTED => 'Ditolak',
        self::STATUS_AUTO_APPROVED => 'Otomatis Disetujui',
    ];

    // Relationships
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class);
    }

    public function attendance(): BelongsTo
    {
        return $this->belongsTo(Attendance::class);
    }

    public function approver(): BelongsTo
    {
        return $this->belongsTo(User::class, 'approved_by');
    }

    // Scopes
    public function scopePending($query)
    {
        return $query->where('status', self::STATUS_PENDING);
    }

    public function scopeApproved($query)
    {
        return $query->whereIn('status', [self::STATUS_APPROVED, self::STATUS_AUTO_APPROVED]);
    }

    public function scopeRejected($query)
    {
        return $query->where('status', self::STATUS_REJECTED);
    }

    public function scopeForUser($query, $userId)
    {
        return $query->where('user_id', $userId);
    }

    public function scopeForCompany($query, $companyId)
    {
        return $query->where('company_id', $companyId);
    }

    // Helpers
    public function isPending(): bool
    {
        return $this->status === self::STATUS_PENDING;
    }

    public function isApproved(): bool
    {
        return in_array($this->status, [self::STATUS_APPROVED, self::STATUS_AUTO_APPROVED]);
    }

    public function isRejected(): bool
    {
        return $this->status === self::STATUS_REJECTED;
    }

    public function getStatusLabelAttribute(): string
    {
        return self::STATUSES[$this->status] ?? $this->status;
    }

    public function getCorrectionTypeLabelAttribute(): string
    {
        return self::CORRECTION_TYPES[$this->correction_type] ?? $this->correction_type;
    }
}
