<?php

namespace App\Http\Resources;

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

class ProviderResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id'                             => $this->id,
            'document'                       => $this->document,
            'rg'                             => $this->rg,
            'user_id'                        => $this->user_id,
            'user'                           => $this->user,
            'average_rating'                 => $this->average_rating,
            'total_services'                 => $this->total_services,
            'birth_date'                     => $this->birth_date ? Carbon::parse($this->birth_date)->format('d/m/Y') : null,
            'gender'                         => $this->gender?->value,
            'gender_label'                   => $this->gender?->label(),
            'selfie_verified'                => $this->selfie_verified,
            'document_verified'              => $this->document_verified,
            'approval_status'                => $this->approval_status?->value ?? $this->approval_status,
            'daily_price_8h'                 => $this->daily_price_8h,
            'daily_price_6h'                 => $this->daily_price_6h,
            'daily_price_4h'                 => $this->daily_price_4h,
            'daily_price_2h'                 => $this->daily_price_2h,
            'profile_media_id'               => $this->profileMedia?->id,
            'recipient_id'                   => $this->recipient_id,
            'recipient_default_bank_account' => $this->recipient_default_bank_account,
            'has_primary_bank_account'       => $this->relationLoaded('primaryBankAccount')
                ? $this->primaryBankAccount !== null
                : $this->primaryBankAccount()->exists(),
            'bank_accounts'                  => ProviderBankAccountResource::collection($this->whenLoaded('bankAccounts')),
            'profile_media'                  => $this->profileMedia ? new MediaResource($this->profileMedia) : null,
            'document_front_media'           => $this->whenLoaded('documentFrontMedia', fn () => $this->documentFrontMedia ? new MediaResource($this->documentFrontMedia) : null),
            'document_back_media'            => $this->whenLoaded('documentBackMedia', fn () => $this->documentBackMedia ? new MediaResource($this->documentBackMedia) : null),
            'created_at'                     => Carbon::parse($this->created_at)->format('d/m/Y H:i'),
            'updated_at'                     => Carbon::parse($this->updated_at)->format('d/m/Y H:i'),
        ];
    }

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