<?php

namespace App\Models;

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

class LastCertificate extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id',
        'image_path',
        'template_name',
        'certificate_data',
        'generated_at',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'certificate_data' => 'array',
        'generated_at' => 'datetime',
    ];

    /**
     * Get the user that owns the certificate.
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the full image URL.
     */
    public function getImageUrlAttribute(): string
    {
        return lastCertificateImageUrl($this->image_path);
    }

    /**
     * Get the storage path for the image.
     */
    public function getStoragePathAttribute(): string
    {
        return resolvePublicStorageFilePath($this->image_path) ?? storage_path('app/public/' . ltrim((string) $this->image_path, '/'));
    }
}
