<?php

namespace App\Http\Resources\Customer;

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

class CustomerResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $data = parent::toArray($request);
        unset($data['images'], $data['cover_image']);

        return [
            ...$data,
            'tenant' => $this->whenLoaded('tenant'),
            'provinsi' => $this->whenLoaded('provinsi'),
            'kabupaten' => $this->whenLoaded('kabupaten'),
            'kecamatan' => $this->whenLoaded('kecamatan'),
            'desa' => $this->whenLoaded('desa'),
            'package_profile' => $this->whenLoaded('packageProfile'),
            'nas_mikrotik' => $this->whenLoaded('nasMikrotik'),
            'billing_profile' => $this->whenLoaded('billingProfile'),
            'images' => $this->whenLoaded('images', fn () => $this->images->map(fn ($image) => [
                'id' => $image->id,
                'stored_file_id' => $image->stored_file_id,
                'uuid' => $image->file?->uuid,
                'original_name' => $image->file?->original_name,
                'category' => $image->category,
                'caption' => $image->caption,
                'is_cover' => $image->is_cover,
                'sort_order' => $image->sort_order,
            ])->values()),
            'cover_image' => $this->whenLoaded('coverImage', fn () => $this->coverImage ? [
                'stored_file_id' => $this->coverImage->stored_file_id,
                'uuid' => $this->coverImage->file?->uuid,
                'original_name' => $this->coverImage->file?->original_name,
                'size' => $this->coverImage->file?->size,
            ] : null),
            'full_installation_address' => collect([
                $this->installation_address,
                $this->desa?->nama,
                $this->kecamatan?->nama,
                $this->kabupaten?->nama,
                $this->provinsi?->nama,
            ])->filter()->implode(', '),
        ];
    }
}
