<?php

namespace App\Models\CRM;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class IdCard extends Model
{
    /** @use HasFactory<\Database\Factories\CRM\IdCardFactory> */
    use HasFactory;

    protected $fillable = [
        'jmbg',
        'date_of_birth',
        'reg_no',
        'issuer',
        'issued_at',
        'expires_at',
        'place_of_birth_city_id',
        'documentable_id',
        'documentable_type',
        'status'
    ]; // this should be polymorphic relation to City model and throut it to Coutry model too.
    protected $dateFormat = 'Y-m-d';
    protected $casts = [
        'date_of_birth' => 'date',
        'issued_at' => 'date',
        'expires_at' => 'date',
    ];

    public function documentable()
    {
        return $this->morphTo();
    }

    public function scopeActive($query)
    {
        return $query->where('status', 'active');
    }

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