<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ProductResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'product_id'   => $this->product_id,
            'plan_id'      => $this->plan_id,
            'plan'         => new PlanResource($this->whenLoaded('plan')),
            // 'variant'      => $this->variant,
            // 'variant_label'=> ucfirst($this->variant),

            // ── EFU Card Block (shown on product card in mobile UI) ────────
            'efu_card' => [
                'variant_label'             => ucfirst($this->variant),
                'efu_product_name'          => $this->efu_product_name,
                'primary_sum_assured_label' => 'Primary Sum Assured (Health Cover)',
                'primary_sum_assured_pkr'   => (float) $this->coverage_per_person,
                'primary_sum_assured_fmt'   => 'Rs. ' . number_format($this->coverage_per_person, 0) . '/-',
                'cashless_hospitalization'  => $this->cashless_hospitalization,
                'contributions'             => $this->when(
                    $this->relationLoaded('contributions'),
                    fn() => $this->contributions->map(fn($c) => [
                        'age_group'        => $c->age_group_label,
                        'contribution_pkr' => (float) $c->contribution_pkr,
                        'contribution_fmt' => 'Rs. ' . number_format($c->contribution_pkr, 0) . '/- Yearly',
                    ])->values()
                ),
            ],

            'coverage' => [
                'coverage_per_person_pkr'          => (float) $this->coverage_per_person,
                'room_board_type'                  => $this->room_board_type,
                'room_board_label'                 => $this->roomBoardLabel(),
                'additional_accident_coverage_pkr' => (float) $this->additional_accident_coverage,
                'icu_coverage'                     => $this->icu_coverage,
                'local_ambulance_covered'          => $this->local_ambulance_covered,
            ],

            'pre_post_hospitalization' => [
                'days'          => $this->pre_post_hospitalization_days,
                'sub_limit_pkr' => (float) $this->pre_post_sub_limit,
            ],

            'emergency_outpatient' => [
                'sub_limit_pkr' => (float) $this->emergency_outpatient_sub_limit,
                'window_hours'  => 48,
            ],

            'pre_existing_coverage' => [
                'year_1_pct' => (float) $this->pre_existing_year1_pct,
                'year_2_pct' => (float) $this->pre_existing_year2_pct,
                'year_3_pct' => (float) $this->pre_existing_year3_pct,
                'note'       => 'Reimbursement only – in line with network hospital benchmark',
            ],

            'additional_benefits' => [
                'accidental_death_benefit_pkr' => (float) $this->accidental_death_benefit,
                'daycare_surgeries'            => true,
                'special_investigations'       => ['MRI', 'CT Scan', 'Echo', 'Thallium Scan'],
                'inadmissible_conditions'      => ['Cancer', 'HIV AIDS'],
                'discount_labs'                => true,
            ],

            'contributions' => ProductContributionResource::collection(
                $this->whenLoaded('contributions')
            ),

            'eligibility' => ProductEligibilityResource::collection(
                $this->whenLoaded('eligibilities')
            ),

            'is_active'  => $this->is_active,
            'created_at' => $this->created_at?->toISOString(),
            'updated_at' => $this->updated_at?->toISOString(),
        ];
    }

    private function roomBoardLabel(): string
    {
        return match ($this->room_board_type) {
            'private'      => 'Private Room',
            'semi_private' => 'Semi-Private Room',
            'general'      => 'General Ward',
            default        => ucfirst($this->room_board_type),
        };
    }
}