<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Region extends Model
{
    use HasFactory, SoftDeletes;

    // 1. Map the custom timestamp columns
    const CREATED_AT = 'created_at_utc';
    const UPDATED_AT = 'updated_at_utc';
    const DELETED_AT = 'deleted_at_utc';

    // 2. Define the table
    protected $table = 'regions';

    // 3. Cast fields automatically
    protected $casts = [
        'country_id' => 'integer',
    ];

    // 4. Only allow these fields to be mass assigned
    protected $fillable = [
        'country_id',
        'name_en',
        'name_native',
        'code',
        'status',
    ];

    // 5. Scope to get only active regions
    public function scopeActive($query)
    {
        return $query->where('status', 'active');
    }

    // 6. Scope to filter by country
    public function scopeByCountry($query, $countryId)
    {
        return $query->where('country_id', $countryId);
    }

    // 7. Relationship: Region belongs to a Country
    public function country()
    {
        return $this->belongsTo(Country::class, 'country_id');
    }
}