<?php

namespace App\Http\Resources\Admin;

use Illuminate\Http\Resources\Json\JsonResource;

class PlanResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'code' => $this->code,
            'plan_date' => $this->plan_date ? $this->plan_date->format('Y-m-d') : null,
            'status' => $this->status,
            'status_label' => $this->status_label,
            'user_id' => $this->user_id,
            'user' => $this->whenLoaded('user', [
                'id' => $this->user->id ?? null,
                'name' => $this->user->name ?? null,
                'full_name' => $this->user->full_name ?? null,
            ]),
            'is_activated' => $this->is_activated,
            'is_deleted' => $this->is_deleted,
            'plan_details' => $this->whenLoaded('planDetails', function () {
                return $this->planDetails->map(function ($detail) {
                    return [
                        'id' => $detail->id,
                        'project_id' => $detail->project_id,
                        'customer_id' => $detail->customer_id,
                        'charge_id' => $detail->charge_id,
                        'type' => $detail->type,
                        'type_label' => $detail->type_label,
                        'time_start' => $detail->time_start ? $detail->time_start->format('H:i') : null,
                        'time_end' => $detail->time_end ? $detail->time_end->format('H:i') : null,
                        'time_range' => $detail->time_range,
                        'formatted_time_range' => $detail->formatted_time_range,
                        'project' => $detail->relationLoaded('project') ? [
                            'id' => $detail->project->id ?? null,
                            'name' => $detail->project->name ?? null,
                        ] : null,
                        'customer' => $detail->relationLoaded('customer') ? [
                            'id' => $detail->customer->id ?? null,
                            'name' => $detail->customer->name ?? null,
                            'first_name' => $detail->customer->first_name ?? null,
                            'last_name' => $detail->customer->last_name ?? null,
                            'full_name' => ($detail->customer->first_name ?? '') . ' ' . ($detail->customer->last_name ?? '')
                        ] : null,
                        'charge' => $detail->relationLoaded('charge') ? [
                            'id' => $detail->charge->id ?? null,
                            'name' => $detail->charge->name ?? null,
                            'first_name' => $detail->charge->first_name ?? null,
                            'last_name' => $detail->charge->last_name ?? null,
                            'full_name' => ($detail->charge->first_name ?? '') . ' ' . ($detail->charge->last_name ?? '')
                        ] : null,
                        'users' => $detail->relationLoaded('users') ? $detail->users->map(function ($user) {
                            return [
                                'id' => $user->id,
                                'name' => $user->name,
                                'full_name' => $user->full_name,
                            ];
                        }) : [],
                        'user_ids' => $detail->relationLoaded('users') ? $detail->users->pluck('id')->toArray() : []
                    ];
                });
            }),
            'plan_details_count' => $this->whenLoaded('planDetails', $this->planDetails->count()),
            'created_at' => isset($this->created_at) ? $this->created_at->format('Y-m-d H:i:s') : null,
            'updated_at' => isset($this->updated_at) ? $this->updated_at->format('Y-m-d H:i:s') : null,
        ];
    }
}