<?php

namespace App\Filters;

use App\Models\Claim\Status;
use App\Models\PensionCase\PensionCase;
use Illuminate\Support\Collection;

class PensionCaseFilterConfig {

    // -----------------------------------------------------------------------
    // Operator sets
    // -----------------------------------------------------------------------

    public static function operatorsForType(string $type): array {
        return match ($type) {
            'boolean' => [
                'is_true'  => 'Is Yes',
                'is_false' => 'Is No',
                'is_empty' => 'Is Empty',
            ],
            'enum', 'select_distinct' => [
                'equals'       => 'Equals',
                'not_equals'   => 'Does Not Equal',
                'is_empty'     => 'Is Empty',
                'is_not_empty' => 'Is Not Empty',
            ],
            'status' => [
                'equals'     => 'Equals',
                'not_equals' => 'Does Not Equal',
                'is_empty'   => 'Is Empty',
            ],
            'date' => [
                'equals'       => 'On',
                'not_equals'   => 'Not On',
                'before'       => 'Before',
                'on_or_before' => 'On or Before',
                'after'        => 'After',
                'on_or_after'  => 'On or After',
                'between'      => 'Between',
                'not_between'  => 'Not Between',
                'is_empty'     => 'Is Empty',
                'is_not_empty' => 'Is Not Empty',
            ],
            'number' => [
                'equals'       => 'Equals',
                'not_equals'   => 'Does Not Equal',
                'greater_than' => 'Greater Than',
                'gte'          => 'Greater Than or Equal',
                'less_than'    => 'Less Than',
                'lte'          => 'Less Than or Equal',
                'between'      => 'Between',
                'not_between'  => 'Not Between',
                'is_empty'     => 'Is Empty',
                'is_not_empty' => 'Is Not Empty',
            ],
            default => [ // string
                'equals'          => 'Equals',
                'not_equals'      => 'Does Not Equal',
                'contains'        => 'Contains',
                'not_contains'    => 'Does Not Contain',
                'starts_with'     => 'Starts With',
                'not_starts_with' => 'Does Not Start With',
                'ends_with'       => 'Ends With',
                'not_ends_with'   => 'Does Not End With',
                'is_empty'        => 'Is Empty',
                'is_not_empty'    => 'Is Not Empty',
            ],
        };
    }

    // -----------------------------------------------------------------------
    // Helpers
    // -----------------------------------------------------------------------

    public static function getField(string $key): array {
        return static::fields()[$key] ?? [];
    }

    public static function allStatuses(): Collection {
        static $cache = null;
        if ($cache === null) {
            $cache = Status::where('type', 'like', 'pension-%')
                ->orderBy('type')
                ->orderBy('sort_order')
                ->get();
        }
        return $cache;
    }

    public static function distinctValues(string $field): array {
        return PensionCase::query()
            ->whereNotNull($field)
            ->where($field, '!=', '')
            ->distinct()
            ->orderBy($field)
            ->pluck($field)
            ->toArray();
    }

    // -----------------------------------------------------------------------
    // Field definitions
    // -----------------------------------------------------------------------

    public static function fields(): array {
        return [

            // ── Identifiers / References ────────────────────────────────────
            'sf_case_id' => [
                'label' => 'SF Case ID',
                'type'  => 'string',
                'group' => 'Identifiers',
            ],
            'case_number' => [
                'label' => 'Case Number',
                'type'  => 'string',
                'group' => 'Identifiers',
            ],
            'national_insurance' => [
                'label' => 'National Insurance',
                'type'  => 'string',
                'group' => 'Identifiers',
            ],
            'cmc_client_reference' => [
                'label' => 'CMC Client Reference',
                'type'  => 'string',
                'group' => 'Identifiers',
            ],
            'fscs_reference' => [
                'label' => 'FSCS Reference',
                'type'  => 'string',
                'group' => 'Identifiers',
            ],
            'fscs_contact_name' => [
                'label' => 'FSCS Contact Name',
                'type'  => 'string',
                'group' => 'Identifiers',
            ],
            'fscs_contact_reference' => [
                'label' => 'FSCS Contact Reference',
                'type'  => 'string',
                'group' => 'Identifiers',
            ],
            'car_reg' => [
                'label' => 'Car Reg',
                'type'  => 'string',
                'group' => 'Identifiers',
            ],
            'atc' => [
                'label' => 'ATC',
                'type'  => 'string',
                'group' => 'Identifiers',
            ],
            'account_name' => [
                'label' => 'Account Name',
                'type'  => 'string',
                'group' => 'Identifiers',
            ],
            'lead_source_cpf' => [
                'label' => 'Lead Source CPF',
                'type'  => 'string',
                'group' => 'Identifiers',
            ],

            // ── Ownership ───────────────────────────────────────────────────
            'case_owner' => [
                'label' => 'Case Owner',
                'type'  => 'select_distinct',
                'group' => 'Ownership',
            ],
            'claims_writer' => [
                'label' => 'Claims Writer',
                'type'  => 'select_distinct',
                'group' => 'Ownership',
            ],
            'file_investigator' => [
                'label' => 'File Investigator',
                'type'  => 'select_distinct',
                'group' => 'Ownership',
            ],
            'created_by' => [
                'label' => 'Created By',
                'type'  => 'select_distinct',
                'group' => 'Ownership',
            ],
            'case_last_modified_by' => [
                'label' => 'Last Modified By',
                'type'  => 'select_distinct',
                'group' => 'Ownership',
            ],
            'tob_issued_by' => [
                'label' => 'TOB Issued By',
                'type'  => 'select_distinct',
                'group' => 'Ownership',
            ],
            'sar_to' => [
                'label' => 'SAR To',
                'type'  => 'select_distinct',
                'group' => 'Ownership',
            ],
            'sar_received_from' => [
                'label' => 'SAR Received From',
                'type'  => 'select_distinct',
                'group' => 'Ownership',
            ],

            // ── Status & Classification ─────────────────────────────────────
            'status_id' => [
                'label' => 'Status',
                'type'  => 'status',
                'group' => 'Status & Classification',
            ],
            'claim_type' => [
                'label'   => 'Claim Type',
                'type'    => 'enum',
                'options' => ['bank', 'fos', 'fscs'],
                'group'   => 'Status & Classification',
            ],
            'settlement_status' => [
                'label' => 'Settlement Status',
                'type'  => 'select_distinct',
                'group' => 'Status & Classification',
            ],
            'bank_claim_status' => [
                'label' => 'Bank Claim Status',
                'type'  => 'select_distinct',
                'group' => 'Status & Classification',
            ],
            'fos_claim_status' => [
                'label' => 'FOS Claim Status',
                'type'  => 'select_distinct',
                'group' => 'Status & Classification',
            ],
            'fscs_claim_status' => [
                'label' => 'FSCS Claim Status',
                'type'  => 'select_distinct',
                'group' => 'Status & Classification',
            ],
            'claim_subject' => [
                'label' => 'Claim Subject',
                'type'  => 'select_distinct',
                'group' => 'Status & Classification',
            ],
            'claim_basis' => [
                'label' => 'Claim Basis',
                'type'  => 'select_distinct',
                'group' => 'Status & Classification',
            ],
            'priority' => [
                'label' => 'Priority',
                'type'  => 'select_distinct',
                'group' => 'Status & Classification',
            ],

            // ── Open / Closed ───────────────────────────────────────────────
            'is_open' => [
                'label' => 'Is Open',
                'type'  => 'boolean',
                'group' => 'Open / Closed',
            ],
            'is_closed' => [
                'label' => 'Is Closed',
                'type'  => 'boolean',
                'group' => 'Open / Closed',
            ],

            // ── Client Flags ────────────────────────────────────────────────
            'vulnerable_client_flag' => [
                'label' => 'Vulnerable Client',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],
            'vulnerable_client_flag_2' => [
                'label' => 'Vulnerable Client (2)',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],
            'special_contact_requirements_flag' => [
                'label' => 'Special Contact Requirements',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],
            'special_contact_requirements_flag_2' => [
                'label' => 'Special Contact Requirements (2)',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],
            'sms_opt_out' => [
                'label' => 'SMS Opt Out',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],
            'do_not_contact' => [
                'label' => 'Do Not Contact',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],
            'ifa_referred' => [
                'label' => 'IFA Referred',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],
            'ifa_refer_opted_out' => [
                'label' => 'IFA Refer Opted Out',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],
            'client_in_dispute' => [
                'label' => 'Client In Dispute',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],
            'sar_sent' => [
                'label' => 'SAR Sent',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],
            'sar_received' => [
                'label' => 'SAR Received',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],
            'fee_capping_flag' => [
                'label' => 'Fee Capping',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],
            'atc_received' => [
                'label' => 'ATC Received',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],
            'pcp_subject' => [
                'label' => 'PCP Subject',
                'type'  => 'boolean',
                'group' => 'Client Flags',
            ],

            // ── Investment ──────────────────────────────────────────────────
            'investment_library' => [
                'label' => 'Investment Library',
                'type'  => 'select_distinct',
                'group' => 'Investment',
            ],
            'underlying_investment' => [
                'label' => 'Underlying Investment',
                'type'  => 'select_distinct',
                'group' => 'Investment',
            ],

            // ── Financial ───────────────────────────────────────────────────
            'quantum' => [
                'label' => 'Quantum (£)',
                'type'  => 'number',
                'group' => 'Financial',
            ],
            'settlement_to_date_new' => [
                'label' => 'Settlement to Date (£)',
                'type'  => 'number',
                'group' => 'Financial',
            ],
            'projected_future_settlement' => [
                'label' => 'Projected Future Settlement (£)',
                'type'  => 'number',
                'group' => 'Financial',
            ],
            'cpfc_fee_percentage' => [
                'label' => 'CPFC Fee %',
                'type'  => 'number',
                'group' => 'Financial',
            ],
            'cpfc_fee_total_nett' => [
                'label' => 'CPFC Fee Total Nett (£)',
                'type'  => 'number',
                'group' => 'Financial',
            ],
            'our_fees_percentage_raw' => [
                'label' => 'Our Fees %',
                'type'  => 'number',
                'group' => 'Financial',
            ],

            // ── Key Dates (datetime) ────────────────────────────────────────
            'opened_at' => [
                'label' => 'Opened At',
                'type'  => 'date',
                'group' => 'Key Dates',
            ],
            'closed_at' => [
                'label' => 'Closed At',
                'type'  => 'date',
                'group' => 'Key Dates',
            ],
            'case_last_modified_at' => [
                'label' => 'Last Modified At',
                'type'  => 'date',
                'group' => 'Key Dates',
            ],

            // ── Process Dates ───────────────────────────────────────────────
            'issued_tob_date' => [
                'label' => 'TOB Issued',
                'type'  => 'date',
                'group' => 'Process Dates',
            ],
            'tob_in_date' => [
                'label' => 'TOB In',
                'type'  => 'date',
                'group' => 'Process Dates',
            ],
            'ifa_referred_date' => [
                'label' => 'IFA Referred Date',
                'type'  => 'date',
                'group' => 'Process Dates',
            ],
            'ifa_refer_opted_out_date' => [
                'label' => 'IFA Opted Out Date',
                'type'  => 'date',
                'group' => 'Process Dates',
            ],
            'application_submitted_date' => [
                'label' => 'Application Submitted',
                'type'  => 'date',
                'group' => 'Process Dates',
            ],
            'date_requested_evidence' => [
                'label' => 'Evidence Requested',
                'type'  => 'date',
                'group' => 'Process Dates',
            ],
            'sar_sent_date' => [
                'label' => 'SAR Sent Date',
                'type'  => 'date',
                'group' => 'Process Dates',
            ],
            'sar_received_date' => [
                'label' => 'SAR Received Date',
                'type'  => 'date',
                'group' => 'Process Dates',
            ],
            'sar_reviewed_scanned_date' => [
                'label' => 'SAR Reviewed / Scanned',
                'type'  => 'date',
                'group' => 'Process Dates',
            ],
            'atc_received_date' => [
                'label' => 'ATC Received Date',
                'type'  => 'date',
                'group' => 'Process Dates',
            ],
            'allocated_to_investigator_date' => [
                'label' => 'Allocated to Investigator',
                'type'  => 'date',
                'group' => 'Process Dates',
            ],
            'application_out_for_signature_date' => [
                'label' => 'App Out for Signature',
                'type'  => 'date',
                'group' => 'Process Dates',
            ],

            // ── FOS Dates ───────────────────────────────────────────────────
            'fos_allocated_date' => [
                'label' => 'FOS Allocated',
                'type'  => 'date',
                'group' => 'FOS Dates',
            ],
            'fos_app_submitted_date' => [
                'label' => 'FOS App Submitted',
                'type'  => 'date',
                'group' => 'FOS Dates',
            ],
            'fos_app_written_sent_to_client_date' => [
                'label' => 'FOS App Written & Sent to Client',
                'type'  => 'date',
                'group' => 'FOS Dates',
            ],
            'fos_complaint_letter_sent_to_client_date' => [
                'label' => 'FOS Complaint Letter Sent to Client',
                'type'  => 'date',
                'group' => 'FOS Dates',
            ],
            'fos_initial_complaint_sent_date' => [
                'label' => 'FOS Initial Complaint Sent',
                'type'  => 'date',
                'group' => 'FOS Dates',
            ],
            'fos_under_review_with_investigator_date' => [
                'label' => 'FOS Under Review with Investigator',
                'type'  => 'date',
                'group' => 'FOS Dates',
            ],

            // ── FSCS Dates ──────────────────────────────────────────────────
            'fscs_allocated_date' => [
                'label' => 'FSCS Allocated',
                'type'  => 'date',
                'group' => 'FSCS Dates',
            ],

            // ── Narrative ───────────────────────────────────────────────────
            'client_settlement_instruction' => [
                'label' => 'Client Settlement Instruction',
                'type'  => 'string',
                'group' => 'Narrative',
            ],
            'reasons_for_not_proceeding' => [
                'label' => 'Reasons for Not Proceeding',
                'type'  => 'string',
                'group' => 'Narrative',
            ],
            'additional_claims_info' => [
                'label' => 'Additional Claims Info',
                'type'  => 'string',
                'group' => 'Narrative',
            ],
            'detail_of_outstanding_evidence' => [
                'label' => 'Detail of Outstanding Evidence',
                'type'  => 'string',
                'group' => 'Narrative',
            ],
            'vulnerable_client_details' => [
                'label' => 'Vulnerable Client Details',
                'type'  => 'string',
                'group' => 'Narrative',
            ],
            'vulnerable_client_details_2' => [
                'label' => 'Vulnerable Client Details (2)',
                'type'  => 'string',
                'group' => 'Narrative',
            ],
            'special_contact_requirements_details' => [
                'label' => 'Special Contact Requirements Details',
                'type'  => 'string',
                'group' => 'Narrative',
            ],
        ];
    }
}
