<?php

namespace App\Domain\Documents\Models;

use App\Domain\Documents\Enums\DocumentCategory;
use App\Domain\Organization\Models\Company;
use App\Support\Concerns\HasUuid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class DocumentType extends Model
{
    use HasFactory, HasUuid, SoftDeletes;

    protected $guarded = ['id', 'uuid'];

    protected function casts(): array
    {
        return [
            'category' => DocumentCategory::class,
            'is_sensitive' => 'boolean',
            'requires_number' => 'boolean',
            'requires_issue_date' => 'boolean',
            'requires_expiry_date' => 'boolean',
            'allows_multiple' => 'boolean',
            'is_active' => 'boolean',
            'reminder_days' => 'array',
            'allowed_mime_types' => 'array',
            'max_file_size_kb' => 'integer',
        ];
    }

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

    public function documents(): HasMany
    {
        return $this->hasMany(Document::class);
    }

    public function requirements(): HasMany
    {
        return $this->hasMany(DocumentRequirement::class);
    }

    /** Falls back to a safe default set when the type declares none. */
    public function acceptedMimeTypes(): array
    {
        return $this->allowed_mime_types ?: config('mams.documents.default_mime_types');
    }

    public function maxFileSizeBytes(): int
    {
        return $this->max_file_size_kb * 1024;
    }
}
