<?php

namespace App\Models\LocalizationManagement;

use App\Models\OrderManagement\Order;
use App\Models\UserManagement\Customer;
use Database\Factories\UserAddressFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Nicolaslopezj\Searchable\SearchableTrait;

class Address extends Model {
    use HasFactory, SearchableTrait;

    protected $table = "localization_mgt__addresses";
    protected $guarded = [];

    const MORPH_ID_COLUMN = "addressable_id";
    const MORPH_TYPE_COLUMN = "addressable_type";
    const MORPH_FUNCTION_NAME = "addressable";


    protected $searchable = [
        'columns' => [
            'localization_mgt__addresses.title' => 10,
            'localization_mgt__addresses.contact_full_name' => 10,
            'localization_mgt__addresses.contact_email' => 10,
            'localization_mgt__addresses.contact_phone' => 10,
            'localization_mgt__addresses.address' => 10,
            'localization_mgt__addresses.address2' => 10,
            'localization_mgt__addresses.zip_code' => 10,
            'localization_mgt__addresses.po_box' => 10,
            'localization_mgt__countries.name' => 10,
            'localization_mgt__states.name' => 10,
            'localization_mgt__cities.name' => 10,
        ],
        'joins' => [
            //'users' => ['users.id', 'user_addresses.user_id'],
            'localization_mgt__countries' => ['localization_mgt__countries.id', 'localization_mgt__addresses.country_id'],
            'localization_mgt__states' => ['localization_mgt__states.id', 'localization_mgt__addresses.state_id'],
            'localization_mgt__cities' => ['localization_mgt__cities.id', 'localization_mgt__addresses.city_id'],
        ]
    ];


    protected static function newFactory(){
        return UserAddressFactory::new();
    }

    public static function validations_attributes(){
        return [
            "title" => __("Title"),
            "address" => __("Address 1"),
            "address2" => __("Address 2"),
            "country_id" => __("Country"),
            "state_id" => __("State"),
            "city_id" => __("City"),
            "quarter_id" => __("Quarter"),
            "zip_code" => __("Zip Code"),
            "po_box" => __("P.O Box"),
            "contact_email" => __("Contact Email"),
            "contact_phone" => __("Contact Phone"),
            "contact_full_name" => __("Contact Full Name"),
        ];
    }

    public static function validations_messages(){
        return  [

        ];
    }

    /** Le Payeur
     * @return MorphTo
     */
    public function addressable(){
        return $this->morphTo(__FUNCTION__,
            self::MORPH_TYPE_COLUMN,self::MORPH_ID_COLUMN);
    }

    public function getDefaultAddressAttribute(): string{
        return $this->attributes['default'] == 1 ? __('Default Address') : '';
    }

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

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

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

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

    public function orders(): HasMany{
        return $this->hasMany(Order::class);
    }


    public function getWarehouseAttribute(){
        return $this->city->warehouse;
    }

    public function getFullTitleAttribute(){
        $city =  $this->city->name ?? "";
        $tittle =  $this->tittle ?? "---";
        return "$city, $tittle";

    }

}
