<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

/**
 * @property int $id
 * @property string $source
 * @property int $source_id
 * @property string $zip_code
 * @property string $address
 * @property bool $has_complement
 * @property string|null $complement
 * @property string|null $nickname
 * @property string|null $instructions
 * @property int|null $city_id
 * @property int|null $state_id
 * @property string $address_type
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property string|null $number
 * @property string|null $district
 * @property bool $is_primary
 * @property float|null $latitude
 * @property float|null $longitude
 * @property-read \App\Models\City|null $city
 * @property-read \App\Models\State|null $state
 * @method static Builder<static>|Address newModelQuery()
 * @method static Builder<static>|Address newQuery()
 * @method static Builder<static>|Address onlyTrashed()
 * @method static Builder<static>|Address query()
 * @method static Builder<static>|Address whereAddress($value)
 * @method static Builder<static>|Address whereAddressType($value)
 * @method static Builder<static>|Address whereCityId($value)
 * @method static Builder<static>|Address whereComplement($value)
 * @method static Builder<static>|Address whereCreatedAt($value)
 * @method static Builder<static>|Address whereDeletedAt($value)
 * @method static Builder<static>|Address whereDistrict($value)
 * @method static Builder<static>|Address whereHasComplement($value)
 * @method static Builder<static>|Address whereId($value)
 * @method static Builder<static>|Address whereInstructions($value)
 * @method static Builder<static>|Address whereIsPrimary($value)
 * @method static Builder<static>|Address whereLatitude($value)
 * @method static Builder<static>|Address whereLongitude($value)
 * @method static Builder<static>|Address whereNickname($value)
 * @method static Builder<static>|Address whereNumber($value)
 * @method static Builder<static>|Address whereSource($value)
 * @method static Builder<static>|Address whereSourceId($value)
 * @method static Builder<static>|Address whereStateId($value)
 * @method static Builder<static>|Address whereUpdatedAt($value)
 * @method static Builder<static>|Address whereZipCode($value)
 * @method static Builder<static>|Address withTrashed(bool $withTrashed = true)
 * @method static Builder<static>|Address withoutTrashed()
 * @mixin \Eloquent
 */
class Address extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'source',
        'source_id',
        'zip_code',
        'address',
        'number',
        'district',
        'has_complement',
        'complement',
        'nickname',
        'instructions',
        'city_id',
        'state_id',
        'address_type',
        'is_primary',
        'latitude',
        'longitude',
    ];

    protected $casts = [
        'has_complement' => 'boolean',
        'is_primary'     => 'boolean',
        'latitude'       => 'float',
        'longitude'      => 'float',
        'created_at'     => 'datetime',
        'updated_at'     => 'datetime',
        'deleted_at'     => 'datetime',
    ];

    public static function preferredForProvider(): Builder
    {
        return static::select('*', DB::raw('ROW_NUMBER() OVER (PARTITION BY source_id ORDER BY is_primary DESC, id DESC) as rn'))
            ->where('source', 'provider');
    }

    public function city()
    {
        return $this->belongsTo(City::class)->select('id', 'name');
    }

    public function state()
    {
        return $this->belongsTo(State::class)->select('id', 'name', 'code');
    }
}
