<?php

namespace App\Models;

use App\Models\User;
use App\Models\City;
use App\Traits\LogOptionsTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Spatie\Activitylog\Traits\LogsActivity;

/**
 * Class Address
 * @property int $user_id
 * @property int $city_id
 * @property string|null $address
 * @property string|null $home
 * @property string|null $landmark
 *
 * @property-read User $user
 * @property-read City $city
 *
 * @method getCity(): string
 * @method getLandmark(): string
 * @method getHome(): string
 * @method getHomeAddress(): string
 * @method getShortAddress(): string
 * @method getFullAddress(): string
 * @method getAddress(): string
 */
class Address extends Model
{
    use LogsActivity, LogOptionsTrait, SoftDeletes;

    protected $guarded = ['id'];

    protected $casts = [
        'user_id' => 'integer',
        'city_id' => 'integer',
        'address' => 'string',
        'home' => 'string',
        'landmark' => 'string',
    ];

    protected static $logName = 'addresses';
    protected static $recordEvents = ['deleted', 'updated'];
    protected static $logOnlyDirty = true;

    protected static $logAttributes = [
        'user_id',
        'city_id',
        'address',
        'home',
        'landmark'
    ];

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

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

    public function getCity(): string
    {
        return (string) $this->city->getName();
    }

    public function getFullAddress(): string|null
    {
        return $this->getCity() . ', ' . $this->address . ', ' . $this->home . ', ' . $this->landmark;
    }

    public function getShortAddress(): string|null
    {
        return $this->getCity() . ', ' . $this->address;
    }

    public function getHomeAddress(): string|null
    {
        return $this->home . ', ' . $this->landmark;
    }

    public function getAddress(): string|null
    {
        return $this->address;
    }

    public function getHome(): string|null
    {
        return $this->home;
    }

    public function getLandmark(): string|null
    {
        return $this->landmark;
    }
}
