<?php

namespace App\Models;

use App\Traits\Auditable;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
use App\Models\Interfaces\IPostCreateAppointmentStructure;
use App\Helper\Helper;


/**
 * @property string $id
 * @property string $identification
 * @property string $firstname
 * @property string $lastname
 * @property string $age
 * @property string $gender
 * @property string $email
 * @property string $phone
 * @property string $address
 * @property string $created_at
 * @property string $updated_at
 */
class Representative extends Model implements IPostCreateAppointmentStructure{
    use Auditable;
    /**
     * The table associated with the model.
     * 
     * @var string
     */
    protected $table = 'representative';

    /**
     * The "type" of the auto-incrementing ID.
     * 
     * @var string
     */
    protected $keyType = 'string';

    /**
     * Indicates if the IDs are auto-incrementing.
     * 
     * @var bool
     */
    public $incrementing = false;

    /**
     * @var array
     */
    protected $fillable = ['id','identification','firstname', 'lastname', 'age', 'gender','address', 'email', 'phone', 'birthday','blood_type','created_at', 'updated_at'];

    public static function getStructureFromRequest(array $data,array $extras=[]):array{
        $representative = [];
        $representative["id"]        = md5($data["nationality"] . $data["identification"] . $data["representativeFirstName"] . $data["representativeLastName"]);
        $representative["firstname"] = $data["representativeFirstName"];
        $representative["lastname"]  = $data["representativeLastName"];
        $representative["age"]       = $data["representativeAge"];
        $representative["gender"]    = $data["represenativeGender"];
        $representative["email"]     = $data["mail"];
        $representative["phone"]     = $data["phone"];
        $representative["address"]   = $data["address"];
        $representative["identification"]   = $data["nationality"] . $data["identification"];
        return $representative;
    }

    /**
     * 
     */
    public static function createOrGetRepresentative($representative):Representative{
        try {
            $representative = Representative::where("identification",$representative["identification"])->firstOrFail();
        } catch (\Throwable $th) {
            $representative     = Representative::create($representative);
        }
        return $representative;
    }

    /**
     * 
     */
    public function createModelFromRequest($data){
        return Helper::getFieldsFromRequest($data,$this->fillable);
    }

    /**
     * Mutador para blood_type que convierte el valor a minúsculas
     */
    public function setBloodTypeAttribute($value)
    {
        $this->attributes['blood_type'] = strtolower($value);
    }

      /**
     * Accesor para blood_type que convierte el valor a mayúsculas
     */
    public function getBloodTypeAttribute($value)
    {
        return strtoupper($value);
    }

     /**
     * Mutador para gender que convierte el valor a minúsculas
     */
    public function setGenderAttribute($value)
    {
        $this->attributes['gender'] = strtolower($value);
    }

    /**
     * Accesor para gender que convierte el valor a mayúsculas
     */
    public function getGenderAttribute($value)
    {
        return strtoupper($value);
    }
    
}
