<?php

namespace App\Http\Resources;

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

class RentalResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id'                  => $this->id,
            'rental_number'       => $this->rental_number,
            'status'              => $this->status,
            'key_status'          => $this->key_status,
            'start_date'          => $this->start_date?->format('Y-m-d'),
            'end_date'            => $this->end_date?->format('Y-m-d'),
            'duration_days'       => $this->duration_days,
            'rental_amount'       => $this->rental_amount,
            'deposit_amount'      => $this->deposit_amount,
            'deposit_collected'   => $this->deposit_collected,
            'deposit_refunded'    => $this->deposit_refunded,
            'deposit_refunded_at' => $this->deposit_refunded_at,
            'key_issued_at'       => $this->key_issued_at,
            'key_returned_at'     => $this->key_returned_at,
            'days_remaining'      => $this->when($this->status === 'active', fn() => $this->daysRemaining()),
            'days_overdue'        => $this->when($this->isExpired(), fn() => $this->daysOverdue()),
            'notes'               => $this->notes,
            'client'              => new ClientResource($this->whenLoaded('client')),
            'locker'              => new LockerResource($this->whenLoaded('locker')),
            'assigned_by'         => $this->whenLoaded('assignedBy', fn() => [
                'id'   => $this->assignedBy->id,
                'name' => $this->assignedBy->name,
            ]),
            'payments'            => PaymentResource::collection($this->whenLoaded('payments')),
            'invoices'            => $this->whenLoaded('invoices'),
            'renewals'            => $this->whenLoaded('renewals'),
            'lost_key_incidents'  => $this->whenLoaded('lostKeyIncidents'),
            'notification_logs'   => $this->whenLoaded('notificationLogs'),
            'created_at'          => $this->created_at,
        ];
    }
}
