<?php

declare(strict_types=1);

namespace App\Models;

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;
use Illuminate\Support\Facades\Storage;

class NovaCardBackground extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'user_id',
        'original_path',
        'processed_path',
        'width',
        'height',
        'mime_type',
        'file_size',
        'sha256',
        'visibility',
    ];

    protected $casts = [
        'width' => 'integer',
        'height' => 'integer',
        'file_size' => 'integer',
    ];

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

    public function cards(): HasMany
    {
        return $this->hasMany(NovaCard::class, 'background_image_id');
    }

    public function processedUrl(): ?string
    {
        if (! $this->processed_path) {
            return null;
        }

        return Storage::disk((string) config('nova_cards.storage.public_disk', 'public'))->url($this->processed_path);
    }
}
