<?php

namespace App\Models;

use App\Models\Concerns\HasPublicUuid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Victim extends Model
{
    use HasFactory, Notifiable, HasPublicUuid;

    protected $table = 'victim';

    protected $fillable = [
        'id', 'crb_number', 'interview_date', 'interview_time', 'refered', 'districtid', 'subcountyid', 'parishid',
        'village', 'survivor_code', 'gender', 'nationality', 'dob', 'nationality_status', 'number_of_children', 'religion', 'marital_status',
        'separeted_children', 'pwd', 'education', 'occupation', 'previos_reporting', 'id_number', 'created_by', 'created_at', 'updated_by',
        'updated_at',
    ];


    protected $casts = [
        'refered' => 'integer',
        'districtid' => 'integer',
        'subcountyid' => 'integer',
        'parishid' => 'integer',
        'gender' => 'integer',
        'nationality' => 'integer',
        'idtype' => 'integer',
        'dob' => 'integer',
        'nationality_status' => 'integer',
        'nationality_status_category_id' => 'integer',
        'number_of_children' => 'integer',
        'religion' => 'integer',
        'marital_status' => 'integer',
        'separeted_children' => 'integer',
        'pwd' => 'integer',
        'disability' => 'integer',
        'education' => 'integer',
        'occupation' => 'integer',
        'employment_status_id' => 'integer',
        'previos_reporting' => 'integer',
        'reporter_sex' => 'integer',
        'reporter_type' => 'integer',
        'reporter_relation' => 'integer',
    ];

    protected static function booted()
    {
        static::saving(function ($victim) {
            if (!empty($victim->crb_number)) {
                $victim->survivor_code = $victim->crb_number;
            }
        });
    }


    public function setNumberOfChildrenAttribute($value): void
    {
        $this->attributes['number_of_children'] = $this->integerDefault($value, 0);
    }

    public function setReferedAttribute($value): void
    {
        $this->attributes['refered'] = $this->booleanDefault($value, 0);
    }

    public function setSeparetedChildrenAttribute($value): void
    {
        $this->attributes['separeted_children'] = $this->booleanDefault($value, 0);
    }

    public function setPwdAttribute($value): void
    {
        $this->attributes['pwd'] = $this->booleanDefault($value, 0);
    }

    public function setPreviosReportingAttribute($value): void
    {
        $this->attributes['previos_reporting'] = $this->booleanDefault($value, 0);
    }

    private function integerDefault($value, int $default = 0): int
    {
        if ($value === null || $value === '') {
            return $default;
        }

        return is_numeric($value) ? (int) $value : $default;
    }

    private function booleanDefault($value, int $default = 0): int
    {
        if ($value === null || $value === '') {
            return $default;
        }

        if (is_bool($value)) {
            return $value ? 1 : 0;
        }

        $value = strtolower(trim((string) $value));

        if (in_array($value, ['1', 'yes', 'true', 'on'], true)) {
            return 1;
        }

        if (in_array($value, ['0', 'no', 'false', 'off'], true)) {
            return 0;
        }

        return $default;
    }

    public function creator()
    {
        return $this->hasOne(User::class, 'id', 'created_by');
    }

    public function getCreator()
    {
        if (!$this->creator) {
            return new User();
        }
        return $this->creator;
    }

    public function lastupdator()
    {
        return $this->hasOne(User::class, 'id', 'updated_by');
    }

    public function getLastupdator()
    {
        if (!$this->lastupdator) {
            return new User();
        }
        return $this->lastupdator;
    }

    public function incidents()
    {
        return $this->hasMany(IncidentData::class, 'victimid', 'id');
    }

    public function perpetrators()
    {
        return $this->hasMany(Perpetrator::class, 'victimid', 'id');
    }

    public function witnesses()
    {
        return $this->hasMany(Witness::class, 'victimid', 'id');
    }

    public function actions()
    {
        return $this->hasMany(Action::class, 'victim_id', 'id');
    }

    public function statuses()
    {
        return $this->hasMany(IncidentStatus::class, 'victimid', 'id');
    }

}
