<?php

namespace App\Http\Resources;

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

class PlateResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $entryDate = $this->plate_entry_date ? Carbon::parse($this->plate_entry_date) : null;
        $months = $entryDate ? $entryDate->diffInMonths(Carbon::now()) : 0;
        
        $antiquity_level = 1; // < 1 mes
        if ($months >= 1 && $months < 3) {
            $antiquity_level = 2;
        } elseif ($months >= 3 && $months < 6) {
            $antiquity_level = 3;
        } elseif ($months >= 6 && $months < 12) {
            $antiquity_level = 4;
        } elseif ($months >= 12) {
            $antiquity_level = 5;
        }

        return [
            'id' => $this->id,
            'plate_name' => $this->plate_name,
            'description' => $this->plate_desc,
            'detail' => $this->plate_detail,
            'location' => $this->plate_location,
            'level' => $this->plate_level,
            'entry_date' => $entryDate ? $entryDate->format('Y-m-d H:i:s') : null,
            'exit_date' => $this->plate_exit_date ? Carbon::parse($this->plate_exit_date)->format('Y-m-d H:i:s') : null,
            'antiquity_level' => $antiquity_level,
        ];
    }
}
