<?php

namespace App\Models\Documents;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class DocumentApproval extends Model
{
    protected $table = 'document_approvals';

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

    public const STATUS_APPROVED = 'APPROVED';

    public const STATUS_REJECTED = 'REJECTED';

    public const STATUS_LABELS = [
        self::STATUS_PENDING => '대기',
        self::STATUS_APPROVED => '승인',
        self::STATUS_REJECTED => '반려',
    ];

    protected $fillable = [
        'document_id',
        'user_id',
        'step',
        'role',
        'status',
        'comment',
        'acted_at',
        'created_by',
        'updated_by',
    ];

    protected $casts = [
        'step' => 'integer',
        'acted_at' => 'datetime',
    ];

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

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

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

    // =========================================================================
    // Accessors
    // =========================================================================

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

    public function getStatusColorAttribute(): string
    {
        return match ($this->status) {
            self::STATUS_PENDING => 'yellow',
            self::STATUS_APPROVED => 'green',
            self::STATUS_REJECTED => 'red',
            default => 'gray',
        };
    }

    // =========================================================================
    // Helper Methods
    // =========================================================================

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

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

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