<?php

namespace App\Models;

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

class Location extends Model
{
    use HasFactory;

    protected $fillable = [
        'city',
        'province',
        'postal_code',
        'latitude',
        'longitude',
    ];

    protected $casts = [
        'latitude' => 'decimal:8',
        'longitude' => 'decimal:8',
    ];

    public static function findByCity(string $city, ?string $province = null): ?self
    {
        $query = self::where('city', 'like', "%{$city}%");
        
        if ($province) {
            $query->where('province', 'like', "%{$province}%");
        }
        
        return $query->first();
    }

    public static function findByPostalCode(string $postalCode): ?self
    {
        return self::where('postal_code', $postalCode)->first();
    }
}
