<?php

namespace App\Http\Resources;

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

class AgentResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->uuid,
            'agent_code' => $this->agent_code,
            'name' => $this->name,
            'local_name' => $this->local_name,
            'photo_url' => $this->photo_path ? url('storage/'.$this->photo_path) : null,
            'agent_type' => $this->agent_type,
            'status' => $this->status->value,
            'status_label' => $this->status->label(),
            'status_reason' => $this->status_reason,
            'can_operate' => $this->status->canOperate(),
            'allowed_transitions' => array_map(
                fn ($status) => ['value' => $status->value, 'label' => $status->label()],
                $this->status->allowedTransitions(),
            ),

            'phone' => $this->phone,
            'whatsapp' => $this->whatsapp,
            'email' => $this->email,
            'district' => $this->district,
            'territory' => $this->territory,

            'joining_date' => $this->joining_date?->toDateString(),
            'contract_start_date' => $this->contract_start_date?->toDateString(),
            'contract_end_date' => $this->contract_end_date?->toDateString(),
            'contract_expired' => $this->contractHasExpired(),
            'monthly_target' => $this->monthly_target,
            'approved_at' => $this->approved_at?->toIso8601String(),

            'branch' => $this->whenLoaded('branch', fn () => $this->branch ? [
                'id' => $this->branch->uuid, 'name' => $this->branch->name,
            ] : null),
            'supervisor' => $this->whenLoaded('supervisor', fn () => $this->supervisor ? [
                'id' => $this->supervisor->uuid, 'name' => $this->supervisor->name,
            ] : null),
            'user' => $this->whenLoaded('user', fn () => $this->user ? [
                'id' => $this->user->uuid, 'email' => $this->user->email,
            ] : null),

            // Payout and identity detail is management-only.
            $this->mergeWhen($request->user()?->can('agent.update'), fn () => [
                'national_id' => $this->national_id,
                'date_of_birth' => $this->date_of_birth?->toDateString(),
                'address' => $this->address,
                'upazila' => $this->upazila,
                'bank_name' => $this->bank_name,
                'bank_branch' => $this->bank_branch,
                'bank_account_name' => $this->bank_account_name,
                'bank_account_number' => $this->bank_account_number,
                'mobile_wallet_provider' => $this->mobile_wallet_provider,
                'mobile_wallet_number' => $this->mobile_wallet_number,
                'notes' => $this->notes,
            ]),

            'status_history' => AgentStatusHistoryResource::collection($this->whenLoaded('statusHistories')),

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