<?php

namespace App\Models;

use App\Models\CSRNow\City;
use App\Models\CSRNow\Country;
use App\Models\CSRNow\State;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Http\Traits\Timezone;

class IdentifyVerification extends Model
{
    use HasFactory, SoftDeletes, Timezone;

    public $pageName = 'Identify Verification List';
    protected $table = "identify_verifications";
    protected $fillable = ['user_id', 'account_type', 'first_name', 'last_name', 'dob', 'gender', 'address', 'address_2', 'city', 'city_id', 'state_id', 'country_id', 'postal_code', 'doc_type', 'doc_number', 'issue_country', 'issue_date', 'expiration_date', 'nationality', 'is_not_pep', 'is_information_correct','quebec_business_number','company_name','federal_business_number', 'social_insurance_number','company_certificate', 'ownership_certificate', 'reference', 'status', 'declined_reason', 'verification_proof', 'duplicate_account_proof', 'approved_steps', 'rejected_steps', 'rejected_steps_reasons', 'final_rejected_steps', 'final_reject_drop_reason', 'final_status', 'deleted_at'];

    protected $dates = ['deleted_at'];
  
    protected $casts = [
        'verification_proof' => "array",
        'company_certificate' => "array",
        'ownership_certificate' => "array",
        'created_at' => 'datetime:d-m-Y H:i:s',
        'issue_date' => 'datetime:d-m-Y H:i:s',
        'expiration_date' => 'datetime:d-m-Y H:i:s'

    ];

    public static function decodeDate($dob)
    {
        return list($year, $month, $day) = explode("-", $dob);
    }

    public static function convertDate($date)
    {
        return date('Y-m-d', strtotime($date));
    }

    public function getAddress()
    {
        return $this->belongsTo(\App\Models\CSRNow\UserAddress::class, 'address_id');
    }
    public function getContactAddress()
    {
        return $this->belongsTo(\App\Models\CSRNow\UserAddress::class, 'contact_address_id');
    }

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

    public function getIssueCountry()
    {
        return $this->belongsTo(Country::class, 'issue_country');
    }

    public function getQuestionAnswer()
    {
        return $this->hasMany(IdentifyQuestionAnswer::class, 'identify_id');
    }

    public function getIdentifyUpdateMessage()
    {
        return $this->hasMany(IdentifyVerificationMessage::class, 'identify_id')->orderBy('created_at', 'desc');
    }

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

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

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

    public function issue_country_detail()
    {
        return $this->belongsTo(Country::class, 'issue_country');
    }

    public function nationality_detail()
    {
        return $this->belongsTo(Country::class, 'nationality');
    }

     public function ApprovedSteps(): Attribute
    {
        return Attribute::make(
            function ($value) {
                return array_filter(explode(',', $value));
            },function ($value){
            if (is_string($value) || empty($value)) {
                return $value;
            } else {
                return implode(',', $value);
            }
        }
        );
    }

    public function RejectedSteps(): Attribute
    {
        return Attribute::make(
            function ($value) {
                return array_filter(explode(',', $value));
            },function ($value){
            if (is_string($value) || empty($value)) {
                return $value;
            } else {
                return implode(',', $value);
            }
        }
        );
    }

    public function FinalRejectedSteps(): Attribute
    {
        return Attribute::make(
            function ($value) {
                return array_filter(explode(',', $value));
            },function ($value){
            if (is_string($value) || empty($value)) {
                return $value;
            } else {
                return implode(',', $value);
            }
        }
        );
    }

}
