<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

class DeliveryArea extends Model
{
    use LogsActivity;

    protected $fillable = [
        'name',
        'cost',
        'estimasi_pengiriman',
        'keterangan',
        'is_active',
    ];

    protected function casts(): array
    {
        return [
            'cost'      => 'decimal:2',
            'is_active' => 'boolean',
        ];
    }

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly(['name', 'cost', 'estimasi_pengiriman', 'is_active'])
            ->logOnlyDirty()
            ->useLogName('delivery_area');
    }

    // ── Relationships ─────────────────────────────────────────────────────

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

    // ── Scopes ────────────────────────────────────────────────────────────

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