<?php

namespace App\Models;

use App\Enums\AdType;
use Database\Factories\AdPlatformFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * Platform/marketplace iklan (RPD §3.1).
 *
 * @property int $id
 * @property string $name
 * @property string $slug
 * @property AdType $ad_type
 * @property string|null $countries comma-separated ISO (ID,MY,PH,TH)
 * @property bool $is_active
 * @property int $sort_order
 *
 * @method static Builder|self active()
 * @method static Builder|self forType(AdType $type)
 */
class AdPlatform extends Model
{
    /** @use HasFactory<AdPlatformFactory> */
    use HasFactory;

    protected $fillable = ['name', 'slug', 'ad_type', 'countries', 'is_active', 'sort_order'];

    protected function casts(): array
    {
        return [
            'ad_type' => AdType::class,
            'is_active' => 'boolean',
            'sort_order' => 'integer',
        ];
    }

    /**
     * Daftar kode negara sebagai array.
     *
     * @return array<int, string>
     */
    public function countryList(): array
    {
        if (! $this->countries) {
            return [];
        }

        return array_map('trim', explode(',', $this->countries));
    }

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

    public function scopeForType(Builder $query, AdType $type): Builder
    {
        return $query->where('ad_type', $type);
    }
}
