<?php

namespace App\Http\Resources\Sales;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Enums\PaymentPlanType;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\Realty\PaymentPlansResource;

class SaleResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'social_reason' => $this->social_reason,
            'nit' => $this->nit,
            'creation_date' => $this->creation_date,
            'payment_method'=> $this->payment_method,
            'delivery_date'=> $this->delivery_date,
            'percentage_initial_fee' => $this->percentage_initial_fee,
            'status' => $this->status,
            'amount'=> $this->amount,
            'initial_fee'=> $this->initial_fee,
            'is_payment_initial_fee'=> $this->is_payment_initial_fee,
            'discount'=> $this->discount,
            'opportunity_id'=> $this->opportunity_id,
            'confirmation_signature'=> $this->confirmation_signature,
            'customer' => $this->opportunity->customer,

            'initial_payments' => PaymentPlansResource::collection(
                $this->payment_plans->where('type', PaymentPlanType::INITIAL_PAYMENT->value)
            ),
            'balance_payments' => PaymentPlansResource::collection(
                $this->payment_plans->where('type', PaymentPlanType::BALANCE->value)
            ),
            'created_at' => $this->created_at ? Carbon::parse($this->created_at)->format('d-m-Y') : null,
            'properties'=> SalesPropertyResource::collection($this->Property),
            'quote_address'=> $this->quote->address,
            'workplace'=> $this->quote->workplace,


        ];
    }
}
