<?php

namespace App\Models\Purchasing;

use App\Models\Concerns\BelongsToTenant;
use App\Models\Geo\City;
use App\Models\Lookups\SupplierAddressType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;

class SupplierAddress extends Model
{
    use BelongsToTenant;

    public $timestamps = true;

    public const UPDATED_AT = null;

    protected $fillable = [
        'uuid', 'supplier_id', 'address_type_id', 'street_line1', 'street_line2',
        'city_id', 'is_primary', 'created_by',
    ];

    protected function casts(): array
    {
        return ['is_primary' => 'boolean'];
    }

    protected static function booted(): void
    {
        static::creating(function (self $model): void {
            if (! $model->uuid) {
                $model->uuid = (string) Str::ulid();
            }
        });
    }

    public function supplier(): BelongsTo
    {
        return $this->belongsTo(Supplier::class);
    }

    public function addressType(): BelongsTo
    {
        return $this->belongsTo(SupplierAddressType::class, 'address_type_id');
    }

    public function city(): BelongsTo
    {
        return $this->belongsTo(City::class);
    }
}
