<?php

namespace App\Models\Documents;

use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 문서 양식(템플릿) 모델
 *
 * @property int $id
 * @property int $tenant_id
 * @property string $name 양식명
 * @property string $category 분류 (품질, 생산 등)
 * @property string|null $title 문서 제목
 * @property string|null $company_name 회사명
 * @property string|null $company_address 회사 주소
 * @property string|null $company_contact 연락처
 * @property string $footer_remark_label 하단 비고 라벨
 * @property string $footer_judgement_label 하단 판정 라벨
 * @property array|null $footer_judgement_options 판정 옵션 (JSON)
 * @property bool $is_active 활성 여부
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @property \Carbon\Carbon|null $deleted_at
 */
class DocumentTemplate extends Model
{
    use BelongsToTenant, SoftDeletes;

    protected $table = 'document_templates';

    protected $fillable = [
        'tenant_id',
        'name',
        'category',
        'title',
        'company_name',
        'company_address',
        'company_contact',
        'footer_remark_label',
        'footer_judgement_label',
        'footer_judgement_options',
        'is_active',
    ];

    protected $casts = [
        'footer_judgement_options' => 'array',
        'is_active' => 'boolean',
    ];

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

    /**
     * 결재라인
     */
    public function approvalLines(): HasMany
    {
        return $this->hasMany(DocumentTemplateApprovalLine::class, 'template_id')
            ->orderBy('sort_order');
    }

    /**
     * 기본 필드
     */
    public function basicFields(): HasMany
    {
        return $this->hasMany(DocumentTemplateBasicField::class, 'template_id')
            ->orderBy('sort_order');
    }

    /**
     * 검사 기준서 섹션
     */
    public function sections(): HasMany
    {
        return $this->hasMany(DocumentTemplateSection::class, 'template_id')
            ->orderBy('sort_order');
    }

    /**
     * 테이블 컬럼
     */
    public function columns(): HasMany
    {
        return $this->hasMany(DocumentTemplateColumn::class, 'template_id')
            ->orderBy('sort_order');
    }

    /**
     * 검사 기준서 동적 필드 정의
     */
    public function sectionFields(): HasMany
    {
        return $this->hasMany(DocumentTemplateSectionField::class, 'template_id')
            ->orderBy('sort_order');
    }

    /**
     * 외부 키 매핑 정의
     */
    public function links(): HasMany
    {
        return $this->hasMany(DocumentTemplateLink::class, 'template_id')
            ->orderBy('sort_order');
    }

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

    /**
     * 활성 양식만
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * 카테고리 필터
     */
    public function scopeCategory($query, string $category)
    {
        return $query->where('category', $category);
    }
}
