<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Storage;

class CullingResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            "id" => $this->id,
            "form_no" => $this->form_no,
            "batch_id" => new BatchResource($this->batch),
            "culling_date" => $this->culling_date,
            "age" => $this->age_in_days,
            "population_before_cull" => $this->population_before_cull,
            "classification" => $this->classification,
            "maliit" => $this->undersized,
            "mahina" => $this->weak,
            "chick_infection" => $this->chick_infection,
            "black_button" => $this->black_button,
            "red_hocks" => $this->red_hocks,
            "dehydrated" => $this->dehydrated,
            "diarrhea" => $this->diarrhea,
            "pilay" => $this->split,
            "split" => $this->split,
            "others" => $this->others,
            "others_text" => $this->others_text,
            "attachments" => $this->attachment_path ? array_map(function ($path, $index) {
                return [
                    'original_name' => $this->attachment_filename[$index] ?? 'Unknown',
                    'path' => $path,
                    'url' => Storage::url($path),
                ];
            }, $this->attachment_path, array_keys($this->attachment_path)) : [],
            "signature" => $this->signature_path ? [
                'original_name' => $this->signature_name,
                'path' => $this->signature_path,
                'url' => Storage::url($this->signature_path),
            ] : null,
        ];
    }
}
