<?php

namespace App\Models;

use App\Models\Traits\FormattedAddressTrait;
use App\Models\Traits\HasUidTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Address extends Model
{
    use FormattedAddressTrait;
    use HasFactory;
    use HasUidTrait;
    use Prunable;

    /** @var string */
    protected $table = 'addresses';

    /** @var string */
    protected $keyType = 'string';

    /** @var bool */
    public $incrementing = false;

    /** @var array */
    protected $fillable = [
        'addressable_type',
        'addressable_id',
        'team_id',
        'street',
        'unit',
        'city',
        'state',
        'postal_code',
        'country',
    ];

    /** @var array */
    protected $hidden = [];

    /** @var array */
    protected $casts = [
        'is_primary' => 'boolean',
    ];

    /** @var array */
    protected $dates = [];

    /** @var array */
    protected $appends = [];

    /** @var array */
    protected $touches = [];

    /** @var array */
    protected $dispatchesEvents = [];

    /*
    |--------------------------------------------------------------------------
    | Class Constants
    |--------------------------------------------------------------------------
    |
    */

    //

    /*
    |--------------------------------------------------------------------------
    | Custom/Private Methods
    |--------------------------------------------------------------------------
    |
    */

    /**
     * Get the prunable model query.
     *
     * @since 1.0.0
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function prunable(): Builder
    {
        //return static::where('created_at', '<=', now()->subMonth());
    }

    /**
     * Prepare the model for pruning.
     *
     * @since 1.0.0
     *
     * @return void
     */
    protected function pruning(): void
    {
        //
    }

    /*
    |--------------------------------------------------------------------------
    | Accessors
    |--------------------------------------------------------------------------
    |
    */

    //

    /*
    |--------------------------------------------------------------------------
    | Mutators
    |--------------------------------------------------------------------------
    |
    */

    //

    /*
    |--------------------------------------------------------------------------
    | Scopes
    |--------------------------------------------------------------------------
    |
    */

    /**
     * Enforce that a given model belongs to a Team.
     *
     * @since 1.0.0
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param int|string $teamId
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeOnTeam($query, $teamId): Builder
    {
        return $query->where('team_id', $teamId);
    }

    /*
    |--------------------------------------------------------------------------
    | Relationships
    |--------------------------------------------------------------------------
    |
    */

    /**
     * Morphable relationship method.
     *
     * @since 1.0.0
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function addressable(): MorphTo
    {
        return $this->morphTo();
    }
}
