<?php

namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Models\PatientConsent;
use App\Models\patientHcpName;
use Spatie\Permission\Traits\HasRoles; // Import HasRoles

class Patient extends Authenticatable implements AuthenticatableContract
{
    use HasFactory, HasRoles;

 
    protected $table = 'patient';

    protected $primaryKey = 'patient_id';
    
    protected $fillable = [
         'patient_id',
        'unique_id',
        'first_name',
        'middle_name',
        'disability',
        'vaccines',
        'last_name',
        'address',
        'date_of_birth',
        'sex',
        'patient_phone_num_country_code',
        'patient_phone_number',
        'patient_er_country_code',
        'patient_er_phone_number',
        'aadhar_card_num',
        'pan_card_num',
        'ssn_card_num',
        'cibil_score',
        'profession',
        'employment_status',
        'marital_status',
        'father_name',
        'mother_name',
        'male_siblings',
        'female_siblings',
        'consent_to_contact',
        'medical_billing_address',
        'current_medications',
        'previous_health_history',
        'consent_to_treatment',
        'allergies',
        'photo',
        'drivers_license',
        'aadhar_card',
        'ssn_card',
        'pan_card',
        'organ_donors',
         'blood_group',
         'abha_number',  
    ];

    public function getAgeAttribute()
    {
        return Carbon::parse($this->date_of_birth)->age;
    }

    public function getFormattedDobAttribute()
    {
        return Carbon::parse($this->date_of_birth)->format('M d, Y');
    }


    public function getConsent(){
        return $this->hasMany(PatientConsent::class, 'patient_id', 'patient_id');
    } 
  
    public function healthHistories()
    {
        return $this->hasMany(PatientHealthHistory::class, 'patient_id', 'patient_id');
    }

    public function hcpNames()
    {
        return $this->hasMany(PatientHcpName::class, 'patient_id', 'patient_id');
    }

    public function labtests()
    {
        return $this->hasMany(Labtest::class, 'patient_id');
    } 

    public function asrs()
{
    return $this->hasMany(Asrs::class);
}
     

    /**
     * Insert multiple patients into the database.
     *
     * @param array $patientsData
     * @return void
     */
    public static function bulkInsert(array $patientsData)
    {
        foreach ($patientsData as $patientData) {
            
            $patientData = array_merge([
                'middle_name' => null,
                'patient_phone_num_country_code' => null,
                'patient_phone_number' => null,
                'patient_er_country_code' => null,
                'patient_er_phone_number' => null,
                'aadhar_card_num' => null,
                'pan_card_num' => null,
                'ssn_card_num' => null,
                'cibil_score' => null,
                'profession' => null,
                'employment_status' => null,
                'marital_status' => null,
                'father_name' => null,
                'mother_name' => null,
                'male_siblings' => null,
                'female_siblings' => null,
                'medical_billing_address' => null,
                'current_medications' => null,
                'previous_health_history' => null,
                'allergies' => null,
                'photo' => null,
                'drivers_license' => null,
                'aadhar_card' => null,
                'ssn_card' => null,
                'pan_card' => null,
            ], $patientData);

            self::create($patientData);
        }
    }
}
