<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Storage;

class ComplianceCertificate extends Model
{
    use HasFactory;

    protected $fillable = [
        'project_id',
        'file_path',
        'file_name',
        'issued_by',
        'issued_date',
        'expires_date',
        'uploaded_by_id',
        'notes',
    ];

    protected $casts = [
        'issued_date' => 'date',
        'expires_date' => 'date',
    ];

    protected $appends = ['url', 'filename'];

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

    public function uploader(): BelongsTo
    {
        return $this->belongsTo(User::class, 'uploaded_by_id');
    }

    public function getUrlAttribute(): string
    {
        return Storage::disk('public')->url($this->file_path);
    }

    public function getFilenameAttribute(): string
    {
        return (string) ($this->getAttributeFromArray('file_name') ?? '');
    }
}
