<?php

namespace App\Http\Resources;

use App\Models\Payment;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Resources\Json\JsonResource;

class PaymentResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id'                 => $this->id,
            'schedule_id'        => $this->schedule_id,
            'schedule'           => new ScheduleResource($this->whenLoaded('schedule')),
            'service_package_id' => $this->service_package_id,
            'schedule_ids'       => $this->whenLoaded('servicePackage', fn () => $this->servicePackage?->items->pluck('schedule_id')->values()),

            'schedules' => $this->whenLoaded('servicePackage', fn () => ScheduleResource::collection(
                $this->servicePackage?->items->pluck('schedule')->filter()->values() ?? collect(),
            )),

            'client_id'                   => $this->client_id,
            'provider_id'                 => $this->provider_id,
            'client_name'                 => $this->client?->user?->name,
            'provider_name'               => $this->provider?->user?->name,
            'client_payment_method_id'    => $this->client_payment_method_id,
            'gateway_provider'            => $this->gateway_provider,
            'gateway_entity_reference'    => $this->gateway_entity_reference,
            'gateway_entity_label'        => $this->gateway_entity_label,
            'gateway_operation_reference' => $this->gateway_operation_reference,
            'gateway_operation_label'     => $this->gateway_operation_label,
            'payment_method'              => $this->payment_method,
            'status'                      => $this->status,
            'gross_amount'                => $this->gross_amount,
            'gateway_fee_amount'          => $this->gateway_fee_amount,
            'platform_fee_amount'         => $this->platform_fee_amount,
            'net_amount'                  => $this->net_amount,
            'currency'                    => $this->currency,
            'installments'                => $this->installments,
            'authorized_at'               => $this->authorized_at?->toISOString(),
            'paid_at'                     => $this->paid_at?->toISOString(),
            'failed_at'                   => $this->failed_at?->toISOString(),
            'cancelled_at'                => $this->cancelled_at?->toISOString(),
            'expires_at'                  => $this->expires_at?->toISOString(),
            'failure_code'                => $this->failure_code,
            'failure_message'             => $this->failure_message,
            'pix'                         => $this->resource->pixData(),
            'gateway_payload'             => $this->gateway_payload,
            'metadata'                    => $this->metadata,
            'created_at'                  => $this->created_at?->toISOString(),
            'updated_at'                  => $this->updated_at?->toISOString(),
        ];
    }

    public static function collection($resource): AnonymousResourceCollection
    {
        return parent::collection($resource);
    }

}
