<?php

namespace App\Exports;

use App\Models\C3CallLog;
use App\Models\C3CaseManagement;
use App\Models\C3PreventionBeneficiary;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\Exportable;


class C3ReportExport implements WithMultipleSheets
{

    public function sheets(): array
    {
        return [
            // Sheet 1: Call Logs
            new class implements FromCollection {
                public function collection()
                {
                    return C3CallLog::all()->map(function ($log, $index) {
                        return [
                            'SN' => $index + 1,
                            'Date' => $log->date,
                            'Call Time' => $log->call_in_time,
                            'Caller ID' => $log->caller_id,
                            'Gender' => $log->gender == 'F' ? 'Female' : 'Male',
                            'Age' => $log->age,
                            'Person with Disability' => $log->person_with_disability ? 'Yes' : 'No',
                            'State' => $log->state,
                            'County' => $log->county,
                            'Payam' => $log->payam,
                            'Boma' => $log->boma,
                            'Rape Case' => $log->rape_case ? 'Yes' : '',
                            'Denial of Resources' => $log->denial_of_resources ? 'Yes' : '',
                            'Psychological/Emotional Abuse' => $log->psychological_emotional_abuse ? 'Yes' : '',
                            'Wife Beating' => $log->wife_beating ? 'Yes' : '',
                            'Child Marriage' => $log->child_marriage ? 'Yes' : '',
                            'Other Incidence Type' => $log->other_incidence_type,
                            'Requested Need' => $log->requested_need_of_gbv_survivor,
                            'Referred To' => $log->referred_to,
                            'Follow-up Required' => $log->follow_up_required ? 'Yes' : 'No',
                            'Follow-up Date' => $log->follow_up_date,
                            'Satisfaction Feedback' => $log->satisfaction_feedback,
                            'Call Attendant' => $log->call_attendant_name,
                        ];
                    });
                }
            },

            // Sheet 2: Case Management
            new class implements FromCollection {
                public function collection()
                {
                    return C3CaseManagement::all()->map(function ($case, $index) {
                        return [
                            'SN' => $index + 1,
                            'Incident ID' => $case->incident_id,
                            'Survivor Code' => $case->survivor_code,
                            'Case Manager Code' => $case->case_manager_code,
                            'Date of Report' => $case->date_of_report,
                            'Date of Incident' => $case->date_of_incident,
                            'Sex' => $case->sex == 'F' ? 'Female' : 'Male',
                            'Ethnicity' => $case->ethnicity,
                            'Country of Origin' => $case->country_of_origin,
                            'Civil/Marital Status' => $case->civil_marital_status,
                            'Person with Disability' => $case->person_with_disability ? 'Yes' : 'No',
                            'GBV Type' => $case->gbv_type,
                            'Incident Location' => $case->incident_location,
                            'State' => $case->incident_area,
                            'County' => $case->incident_sub_area,
                            'Camp/Town' => $case->incident_camp_town,
                            'Number of Perpetrators' => $case->number_of_alleged_perpetrators,
                            'Perpetrator Relationship' => $case->alleged_perpetrator_relationship,
                            'Safe House/Shelter' => $case->safe_house_shelter,
                            'Health/Medical Services' => $case->health_medical_services,
                            'Psychosocial Services' => $case->psychosocial_services,
                            'Legal Assistance' => $case->legal_assistance_services,
                            'Consent for Information Sharing' => $case->consent_given_for_information_sharing ? 'Yes' : 'No',
                        ];
                    });
                }
            },

            // Sheet 3: Prevention Beneficiaries
            new class implements FromCollection {
                public function collection()
                {
                    return C3PreventionBeneficiary::all()->map(function ($beneficiary, $index) {
                        return [
                            'SN' => $index + 1,
                            'Name' => $beneficiary->name,
                            'Gender' => $beneficiary->gender,
                            'Age' => $beneficiary->age,
                            'Category' => $beneficiary->category_of_participants,
                            'Person with Disability' => $beneficiary->person_with_disability ? 'Yes' : 'No',
                            'Refugee/Displaced Status' => $beneficiary->refugee_displaced_status,
                            'State' => $beneficiary->state,
                            'County' => $beneficiary->county,
                            'Payam' => $beneficiary->payam,
                            'Boma' => $beneficiary->boma,
                            'Prevention Messages' => $beneficiary->prevention_messages ? 'Yes' : '',
                            'Referral Pathways' => $beneficiary->referral_pathways ? 'Yes' : '',
                            'Conflict Resolution' => $beneficiary->conflict_resolution ? 'Yes' : '',
                            'Male Engagement' => $beneficiary->male_engagement ? 'Yes' : '',
                            'Positive Masculinity Activity' => $beneficiary->positive_masculinity_activity ? 'Yes' : '',
                            'IEC Materials' => $beneficiary->received_iec_materials ? 'Yes' : '',
                            'Radio Talk Shows' => $beneficiary->received_radio_talkshows ? 'Yes' : '',
                            'Other Activities' => $beneficiary->other_activities_support,
                        ];
                    });
                }
            },

            // Sheet 4: Summary Dashboard
            new class implements FromCollection {
                public function collection()
                {
                    $totalCalls = C3CallLog::count();
                    $totalCases = C3CaseManagement::count();
                    $totalPrevention = C3PreventionBeneficiary::count();
                    $satisfiedSurvivors = C3CallLog::where('satisfaction_feedback', 'Yes')->count();
                    $satisfactionRate = $totalCalls > 0 ? round(($satisfiedSurvivors / $totalCalls) * 100, 1) : 0;

                    $gbvTypes = [
                        'Rape' => C3CallLog::where('rape_case', true)->count() + C3CaseManagement::where('gbv_type', 'like', '%Rape%')->count(),
                        'Denial of Resources' => C3CallLog::where('denial_of_resources', true)->count() + C3CaseManagement::where('gbv_type', 'like', '%Denial%')->count(),
                        'Psychological/Emotional Abuse' => C3CallLog::where('psychological_emotional_abuse', true)->count(),
                        'Wife Beating' => C3CallLog::where('wife_beating', true)->count(),
                        'Child Marriage' => C3CallLog::where('child_marriage', true)->count(),
                    ];

                    $rows = collect();
                    $rows->push(['Component 3 - GBV Response Summary Report']);
                    $rows->push([]);
                    $rows->push(['Key Performance Indicators']);
                    $rows->push(['Indicator', 'Value']);
                    $rows->push(['Total Calls Received (Hotline)', $totalCalls]);
                    $rows->push(['Total GBV Cases Managed', $totalCases]);
                    $rows->push(['Community Members Reached (Prevention)', $totalPrevention]);
                    $rows->push(['Survivors Satisfied with Services', $satisfiedSurvivors . ' (' . $satisfactionRate . '%)']);
                    $rows->push([]);
                    $rows->push(['GBV Types Distribution']);
                    $rows->push(['Type', 'Count']);
                    foreach ($gbvTypes as $type => $count) {
                        $rows->push([$type, $count]);
                    }

                    return $rows;
                }
            },

            // Sheet 5: Dropdowns
            new class implements FromCollection {
                public function collection()
                {
                    return collect([
                        ['Gender', 'Category', 'GBV Type', 'Referral Status', 'Disability Status'],
                        ['Male', 'Adult Male', 'Rape', 'Service Provided', 'Yes'],
                        ['Female', 'Adult Female', 'Denial of Resources', 'Referral Declined', 'No'],
                        ['', 'Adolescent Boy', 'Psychological Abuse', 'Services Already Received', ''],
                        ['', 'Adolescent Girl', 'Wife Beating', 'Service Unavailable', ''],
                        ['', 'Community Leader', 'Child Marriage', '', ''],
                        ['', 'Religious Leader', 'Other', '', ''],
                    ]);
                }
            },
        ];
    }
}