<?php

namespace App\Models\Documents;

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

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 TYPE_LABELS = [
        self::TYPE_GENERAL => '일반',
        self::TYPE_SIGNATURE => '서명',
        self::TYPE_IMAGE => '이미지',
        self::TYPE_REFERENCE => '참고자료',
    ];

    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');
    }

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

    public function getTypeLabelAttribute(): string
    {
        return self::TYPE_LABELS[$this->attachment_type] ?? $this->attachment_type;
    }
}
