<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class MediaStorageTarget extends Model
{
    use HasFactory;

    protected $fillable = [
        'key',
        'name',
        'driver',
        'config',
        'is_hot',
        'is_default',
        'is_active',
        'priority',
    ];

    protected $casts = [
        'config' => 'array',
        'is_hot' => 'boolean',
        'is_default' => 'boolean',
        'is_active' => 'boolean',
        'priority' => 'integer',
    ];

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('is_active', true);
    }

    public function scopeHot(Builder $query): Builder
    {
        return $query->where('is_hot', true);
    }

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

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

    public function toFilesystemConfig(): array
    {
        $config = $this->config ?? [];

        $base = [
            'driver' => $this->driver,
            'throw' => false,
            'report' => false,
        ];

        return array_merge($base, $config);
    }
}

