<?php

namespace App\Models\Construction;

use App\Models\Members\User;
use App\Models\Site;
use App\Traits\Auditable;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 구조검토 모델
 *
 * @property int $id
 * @property int $tenant_id
 * @property string|null $review_number
 * @property int|null $partner_id
 * @property string|null $partner_name
 * @property int|null $site_id
 * @property string|null $site_name
 * @property string|null $request_date
 * @property string|null $review_company
 * @property string|null $reviewer_name
 * @property string|null $review_date
 * @property string|null $completion_date
 * @property string $status
 * @property string|null $file_url
 * @property string|null $remarks
 * @property bool $is_active
 * @property int|null $created_by
 * @property int|null $updated_by
 * @property int|null $deleted_by
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 */
class StructureReview extends Model
{
    use Auditable, BelongsToTenant, ModelTrait, SoftDeletes;

    protected $table = 'structure_reviews';

    // 상태 상수
    public const STATUS_PENDING = 'pending';

    public const STATUS_COMPLETED = 'completed';

    protected $fillable = [
        'tenant_id',
        'review_number',
        'partner_id',
        'partner_name',
        'site_id',
        'site_name',
        'request_date',
        'review_company',
        'reviewer_name',
        'review_date',
        'completion_date',
        'status',
        'file_url',
        'remarks',
        'is_active',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    protected $casts = [
        'request_date' => 'date:Y-m-d',
        'review_date' => 'date:Y-m-d',
        'completion_date' => 'date:Y-m-d',
        'is_active' => 'boolean',
    ];

    protected $attributes = [
        'is_active' => true,
        'status' => self::STATUS_PENDING,
    ];

    // =========================================================================
    // 관계 정의
    // =========================================================================

    /**
     * 현장
     */
    public function site(): BelongsTo
    {
        return $this->belongsTo(Site::class, 'site_id');
    }

    /**
     * 생성자
     */
    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * 수정자
     */
    public function updater(): BelongsTo
    {
        return $this->belongsTo(User::class, 'updated_by');
    }

    // =========================================================================
    // 스코프
    // =========================================================================

    /**
     * 상태별 필터
     */
    public function scopeStatus($query, string $status)
    {
        return $query->where('status', $status);
    }

    /**
     * 현장별 필터
     */
    public function scopeSite($query, int $siteId)
    {
        return $query->where('site_id', $siteId);
    }

    /**
     * 거래처별 필터
     */
    public function scopePartner($query, int $partnerId)
    {
        return $query->where('partner_id', $partnerId);
    }

    // =========================================================================
    // 헬퍼 메서드
    // =========================================================================

    /**
     * 상태 라벨 반환
     */
    public function getStatusLabelAttribute(): string
    {
        return match ($this->status) {
            self::STATUS_PENDING => '검토대기',
            self::STATUS_COMPLETED => '검토완료',
            default => $this->status,
        };
    }

    /**
     * 검토대기 여부
     */
    public function isPending(): bool
    {
        return $this->status === self::STATUS_PENDING;
    }

    /**
     * 검토완료 여부
     */
    public function isCompleted(): bool
    {
        return $this->status === self::STATUS_COMPLETED;
    }
}
