<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;

class Address extends Model
{
    use HasFactory;
    use SoftDeletes;
    use LogsActivity;

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults();
   }

    protected $table = 'address';

    protected $fillable = [
        'label',
        'name',
        'address',
        'province_id',
        'city_id',
        'district_id',
        'subdistrict_id',
        'province_name',
        'city_name',
        'district_name',
        'subdistrict_name',
        'postal_code',
        'phone',
        'longitude',
        'latitude',
        'user_id',
        'is_primary'
    ];

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function province()
    {
        return $this->belongsTo(Province::class, 'province_id', 'id');
    }

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

    public function district()
    {
        return $this->belongsTo(District::class, 'district_id', 'id');
    }

    public function subdistrict()
    {
        return $this->belongsTo(Subdistrict::class, 'subdistrict_id', 'id');
    }


    public function getLocationAttribute()
    {
        $province = $this->province?->name;
        $city = $this->city?->name;
        $district = $this->district?->name;
        $subdistrict = $this->subdistrict?->name;
        $postalCode = $this->postal_code;
        
        return "{$province}, {$city}, {$district}, {$subdistrict}, {$postalCode}";
    }
}
