<?php

namespace App\Models\Documents;

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

/**
 * 문서 첨부파일 모델
 *
 * @property int $id
 * @property int $document_id
 * @property int $file_id
 * @property string $attachment_type 첨부 유형 (general, signature, image 등)
 * @property string|null $description 설명
 * @property int|null $created_by
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 */
class DocumentAttachment extends Model
{
    protected $table = 'document_attachments';

    // =========================================================================
    // 첨부 유형 상수
    // =========================================================================

    public const TYPE_GENERAL = 'general';

    public const TYPE_SIGNATURE = 'signature';

    public const TYPE_IMAGE = 'image';

    public const TYPE_REFERENCE = 'reference';

    public const TYPES = [
        self::TYPE_GENERAL,
        self::TYPE_SIGNATURE,
        self::TYPE_IMAGE,
        self::TYPE_REFERENCE,
    ];

    // =========================================================================
    // Fillable & Casts
    // =========================================================================

    protected $fillable = [
        'document_id',
        'file_id',
        'attachment_type',
        'description',
        'created_by',
    ];

    protected $attributes = [
        'attachment_type' => self::TYPE_GENERAL,
    ];

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

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

    /**
     * 파일
     */
    public function file(): BelongsTo
    {
        return $this->belongsTo(File::class);
    }

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

    // =========================================================================
    // Scopes
    // =========================================================================

    /**
     * 특정 유형의 첨부파일
     */
    public function scopeOfType($query, string $type)
    {
        return $query->where('attachment_type', $type);
    }

    /**
     * 일반 첨부파일
     */
    public function scopeGeneral($query)
    {
        return $query->where('attachment_type', self::TYPE_GENERAL);
    }

    /**
     * 서명 첨부파일
     */
    public function scopeSignatures($query)
    {
        return $query->where('attachment_type', self::TYPE_SIGNATURE);
    }

    /**
     * 이미지 첨부파일
     */
    public function scopeImages($query)
    {
        return $query->where('attachment_type', self::TYPE_IMAGE);
    }

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

    /**
     * 서명 첨부파일 여부
     */
    public function isSignature(): bool
    {
        return $this->attachment_type === self::TYPE_SIGNATURE;
    }

    /**
     * 이미지 첨부파일 여부
     */
    public function isImage(): bool
    {
        return $this->attachment_type === self::TYPE_IMAGE;
    }
}
