<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class TibaLetaSeeder extends Seeder
{
    public function run()
    {
        $now = date('Y-m-d H:i:s');
        $password = password_hash('TibaLeta123!', PASSWORD_DEFAULT);
        $this->db->table('users')->insertBatch([
            ['full_name'=>'Amina Njoroge','email'=>'admin@tibaleta.test','phone'=>'+254700000001','password_hash'=>$password,'role'=>'admin','status'=>'active','email_verified_at'=>$now,'created_at'=>$now,'updated_at'=>$now],
            ['full_name'=>'Sarah Wanjiku','email'=>'patient@tibaleta.test','phone'=>'+254700000002','password_hash'=>$password,'role'=>'patient','status'=>'active','email_verified_at'=>$now,'created_at'=>$now,'updated_at'=>$now],
            ['full_name'=>'Dr. Jane Mwangi','email'=>'doctor@tibaleta.test','phone'=>'+254700000003','password_hash'=>$password,'role'=>'practitioner','status'=>'active','email_verified_at'=>$now,'created_at'=>$now,'updated_at'=>$now],
            ['full_name'=>'Nurse Mary Wanjiku','email'=>'nurse@tibaleta.test','phone'=>'+254700000004','password_hash'=>$password,'role'=>'practitioner','status'=>'active','email_verified_at'=>$now,'created_at'=>$now,'updated_at'=>$now],
            ['full_name'=>'Dr. Daniel Otieno','email'=>'paeds@tibaleta.test','phone'=>'+254700000005','password_hash'=>$password,'role'=>'practitioner','status'=>'active','email_verified_at'=>$now,'created_at'=>$now,'updated_at'=>$now],
            ['full_name'=>'TibaLeta Demo Lab','email'=>'partner@tibaleta.test','phone'=>'+254700000006','password_hash'=>$password,'role'=>'partner','status'=>'active','email_verified_at'=>$now,'created_at'=>$now,'updated_at'=>$now],
        ]);
        $ids=[]; foreach($this->db->table('users')->select('id,email')->get()->getResultArray() as $u)$ids[$u['email']]=$u['id'];
        $this->db->table('patient_profiles')->insert(['user_id'=>$ids['patient@tibaleta.test'],'preferred_language'=>'English','created_at'=>$now,'updated_at'=>$now]);
        $patientId=(int)$this->db->insertID();
        $services=[
            ['Doctor Home Visit','doctor-home-visit','General practitioner care at home.',2500,45],['Nursing Care','nursing-care','Professional nursing procedures and monitoring.',1800,45],['Physiotherapy','physiotherapy','Home rehabilitation and mobility support.',2200,60],['Lab Sample Collection','lab-sample-collection','Home sample collection with partner laboratories.',1200,30],['Elderly Care','elderly-care','Scheduled support for older adults.',2000,60],['Teleconsultation','teleconsultation','Secure virtual consultation with a practitioner.',1200,30],['Maternal Care','maternal-care','Prenatal and postnatal home support.',2200,60],['Chronic Care','chronic-care','Ongoing monitoring and follow-up at home.',2000,45]
        ];
        foreach($services as $i=>$s)$this->db->table('services')->insert(['name'=>$s[0],'slug'=>$s[1],'description'=>$s[2],'base_price'=>$s[3],'duration_minutes'=>$s[4],'is_active'=>1,'sort_order'=>$i+1,'created_at'=>$now,'updated_at'=>$now]);
        $serviceMap=[]; foreach($this->db->table('services')->select('id,slug')->get()->getResultArray() as $s)$serviceMap[$s['slug']]=$s['id'];
        $this->db->table('specialties')->insertBatch([
            ['name'=>'General Practice','slug'=>'general-practice','is_active'=>1,'created_at'=>$now,'updated_at'=>$now],['name'=>'Nursing','slug'=>'nursing','is_active'=>1,'created_at'=>$now,'updated_at'=>$now],['name'=>'Paediatrics','slug'=>'paediatrics','is_active'=>1,'created_at'=>$now,'updated_at'=>$now],['name'=>'Physiotherapy','slug'=>'physiotherapy','is_active'=>1,'created_at'=>$now,'updated_at'=>$now]
        ]);
        $profiles=[
            [$ids['doctor@tibaleta.test'],'KMPDC-AT345','Kenya Medical Practitioners and Dentists Council','General Practitioner','Compassionate GP with experience in family medicine, preventive health and chronic-care support.',8,4.9,184],
            [$ids['nurse@tibaleta.test'],'NCK-21981','Nursing Council of Kenya','Registered Nurse','Home nursing, wound care, medication support and recovery monitoring.',6,4.8,96],
            [$ids['paeds@tibaleta.test'],'KMPDC-P723','Kenya Medical Practitioners and Dentists Council','Paediatrician','Child-focused home assessment and follow-up care.',10,4.7,132]
        ];
        foreach($profiles as $p){$this->db->table('practitioner_profiles')->insert(['user_id'=>$p[0],'registration_number'=>$p[1],'professional_body'=>$p[2],'profession'=>$p[3],'bio'=>$p[4],'years_experience'=>$p[5],'verification_status'=>'verified','is_online'=>1,'rating'=>$p[6],'review_count'=>$p[7],'service_radius_km'=>25,'verified_at'=>$now,'verified_by'=>$ids['admin@tibaleta.test'],'created_at'=>$now,'updated_at'=>$now]);}
        $pp=[]; foreach($this->db->table('practitioner_profiles')->select('id,user_id')->get()->getResultArray() as $p)$pp[$p['user_id']]=$p['id'];
        $locations=[[$pp[$ids['doctor@tibaleta.test']],-1.286389,36.817223],[$pp[$ids['nurse@tibaleta.test']],-1.292066,36.821946],[$pp[$ids['paeds@tibaleta.test']],-1.278500,36.812200]];
        foreach($locations as $l)$this->db->table('practitioner_locations')->insert(['practitioner_id'=>$l[0],'latitude'=>$l[1],'longitude'=>$l[2],'is_current'=>1,'captured_at'=>$now,'created_at'=>$now,'updated_at'=>$now]);
        foreach([$pp[$ids['doctor@tibaleta.test']],$pp[$ids['paeds@tibaleta.test']]] as $pid)$this->db->table('practitioner_services')->insert(['practitioner_id'=>$pid,'service_id'=>$serviceMap['doctor-home-visit'],'price'=>2500,'is_active'=>1,'created_at'=>$now,'updated_at'=>$now]);
        $this->db->table('practitioner_services')->insert(['practitioner_id'=>$pp[$ids['nurse@tibaleta.test']],'service_id'=>$serviceMap['nursing-care'],'price'=>1500,'is_active'=>1,'created_at'=>$now,'updated_at'=>$now]);
        $this->db->table('practitioner_services')->insert(['practitioner_id'=>$pp[$ids['doctor@tibaleta.test']],'service_id'=>$serviceMap['teleconsultation'],'price'=>1200,'is_active'=>1,'created_at'=>$now,'updated_at'=>$now]);
        $this->db->table('partners')->insertBatch([
            ['user_id'=>$ids['partner@tibaleta.test'],'name'=>'TibaLeta Demo Lab','type'=>'lab','email'=>'lab@partner.test','phone'=>'+254700100100','address'=>'Nairobi','status'=>'active','integration_status'=>'manual','created_at'=>$now,'updated_at'=>$now],
            ['name'=>'TibaLeta Demo Pharmacy','type'=>'pharmacy','email'=>'pharmacy@partner.test','phone'=>'+254700200200','address'=>'Nairobi','status'=>'active','integration_status'=>'manual','created_at'=>$now,'updated_at'=>$now]
        ]);
        $this->db->table('family_members')->insert(['patient_id'=>$patientId,'full_name'=>'Grace Wanjiku','relationship'=>'Mother','date_of_birth'=>'1958-04-12','status'=>'active','created_at'=>$now,'updated_at'=>$now]);
        $this->db->table('care_packages')->insertBatch([
            ['name'=>'Senior Care','slug'=>'senior-care','description'=>'Recurring nurse check-ins and coordinated clinician review for older adults.','price'=>8500,'billing_period'=>'monthly','included_services'=>json_encode(['2 nursing visits','1 virtual clinician review']),'is_active'=>1,'created_at'=>$now,'updated_at'=>$now],
            ['name'=>'Recovery at Home','slug'=>'recovery-at-home','description'=>'Structured post-discharge nursing and rehabilitation support.','price'=>12000,'billing_period'=>'monthly','included_services'=>json_encode(['2 nursing visits','2 physiotherapy sessions']),'is_active'=>1,'created_at'=>$now,'updated_at'=>$now],
            ['name'=>'Chronic Care','slug'=>'chronic-care-plan','description'=>'Regular monitoring and follow-up for ongoing conditions.','price'=>7000,'billing_period'=>'monthly','included_services'=>json_encode(['2 home monitoring visits','1 teleconsultation']),'is_active'=>1,'created_at'=>$now,'updated_at'=>$now]
        ]);
        $this->db->table('care_requests')->insert(['patient_id'=>$patientId,'service_id'=>$serviceMap['doctor-home-visit'],'scheduled_for'=>date('Y-m-d H:i:s',time()+7200),'urgency'=>'routine','status'=>'accepted','chief_complaint'=>'General consultation and follow-up at home.','latitude'=>-1.28333,'longitude'=>36.81667,'address_text'=>'Kilimani, Nairobi','estimated_amount'=>2500,'assigned_practitioner_id'=>$pp[$ids['doctor@tibaleta.test']],'accepted_at'=>$now,'created_at'=>$now,'updated_at'=>$now]);
        $acceptedRequest=(int)$this->db->insertID();
        $this->db->table('care_request_assignments')->insert(['care_request_id'=>$acceptedRequest,'practitioner_id'=>$pp[$ids['doctor@tibaleta.test']],'status'=>'accepted','offered_at'=>$now,'responded_at'=>$now,'created_at'=>$now,'updated_at'=>$now]);
        $this->db->table('visits')->insert(['care_request_id'=>$acceptedRequest,'patient_id'=>$patientId,'practitioner_id'=>$pp[$ids['doctor@tibaleta.test']],'status'=>'scheduled','created_at'=>$now,'updated_at'=>$now]);
        $this->db->table('care_requests')->insert(['patient_id'=>$patientId,'service_id'=>$serviceMap['nursing-care'],'scheduled_for'=>date('Y-m-d H:i:s',time()+86400),'urgency'=>'routine','status'=>'matching','chief_complaint'=>'Scheduled home nursing support.','latitude'=>-1.29200,'longitude'=>36.82100,'address_text'=>'Hurlingham, Nairobi','estimated_amount'=>1800,'created_at'=>$now,'updated_at'=>$now]);
        $this->db->table('appointments')->insert(['care_request_id'=>$acceptedRequest,'patient_id'=>$patientId,'practitioner_id'=>$pp[$ids['doctor@tibaleta.test']],'type'=>'follow_up','starts_at'=>date('Y-m-d H:i:s',time()+172800),'ends_at'=>date('Y-m-d H:i:s',time()+174600),'status'=>'scheduled','created_at'=>$now,'updated_at'=>$now]);
        $this->db->table('notifications')->insert(['user_id'=>$ids['patient@tibaleta.test'],'title'=>'Welcome to TibaLeta','message'=>'Your family healthcare dashboard is ready.','type'=>'welcome','action_url'=>'/patient','created_at'=>$now,'updated_at'=>$now]);
    }
}
