<?php

namespace App\Models\Qualitys;

use App\Models\Members\User;
use App\Traits\Auditable;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class PerformanceReport extends Model
{
    use Auditable, BelongsToTenant, SoftDeletes;

    protected $table = 'performance_reports';

    const STATUS_UNCONFIRMED = 'unconfirmed';

    const STATUS_CONFIRMED = 'confirmed';

    const STATUS_REPORTED = 'reported';

    protected $fillable = [
        'tenant_id',
        'quality_document_id',
        'year',
        'quarter',
        'confirmation_status',
        'confirmed_date',
        'confirmed_by',
        'memo',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    protected $casts = [
        'confirmed_date' => 'date',
        'year' => 'integer',
        'quarter' => 'integer',
    ];

    // ===== Relationships =====

    public function qualityDocument()
    {
        return $this->belongsTo(QualityDocument::class);
    }

    public function confirmer()
    {
        return $this->belongsTo(User::class, 'confirmed_by');
    }

    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    // ===== Status Helpers =====

    public function isUnconfirmed(): bool
    {
        return $this->confirmation_status === self::STATUS_UNCONFIRMED;
    }

    public function isConfirmed(): bool
    {
        return $this->confirmation_status === self::STATUS_CONFIRMED;
    }

    public function isReported(): bool
    {
        return $this->confirmation_status === self::STATUS_REPORTED;
    }
}
