<?php

namespace App\Http\Resources\Opportunity;

use App\Enums\PaymentPlanType;
use App\Http\Resources\CustomerReferenceResource;
use App\Http\Resources\SourceResource;
use Illuminate\Http\Request;
use App\Http\Resources\User\UserResource;
use App\Http\Resources\Stage\StageResource;
use App\Http\Resources\MethodContactResource;
use App\Http\Resources\Realty\PropertyResource;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\Customer\CustomerResource;
use App\Http\Resources\Opportunity\Procedure\ProcedureResource;
use App\Http\Resources\Quote\QuoteResource;
use App\Http\Resources\Realty\PaymentPlansResource;
use App\Http\Resources\Sales\SaleResource;

class OpportunityResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $initialPayments = collect();
        $balancePayments = collect();
        $sales  = $this->sales->where('status', '!=', 'DISCARD');

        // Recorremos todas las ventas asociadas a la oportunidad
        foreach ($sales as $sale) {
            // Filtramos los planes de pago según su tipo (inicial y saldo) y los agrupamos
        if($sale->status != "DISCARD" )
            $initialPayments = $initialPayments->merge(
                $sale->payment_plans->where('type', PaymentPlanType::INITIAL_PAYMENT->value)
            );

            $balancePayments = $balancePayments->merge(
                $sale->payment_plans->where('type', PaymentPlanType::BALANCE->value)
            );
        }
        return [
            'id' => $this->id,
            'start_date' => $this->start_date,
            'end_date' => $this->end_date,
            'description' => $this->description,

            'source_id' => $this->source_id,
            'source'=> new SourceResource($this->source),

            'contact_method_id' => $this->contact_method_id,
            'contact_method'=> new MethodContactResource($this->contact_method),

            'customer_references_id' => $this->customer_references_id,
            'customer_references'=> new CustomerReferenceResource($this->customer_reference),

            'user_id' => $this->user_id,
            'user'=> new UserResource($this->user),

            'stage_id' => $this->stage_id,
            'stage'=> new StageResource($this->stage),

            'property_id' => $this->property_id,
            'property'=>new PropertyResource( $this->property),

            'customer_id' => $this->customer_id,
            'customer'=> new CustomerResource($this->customer),

            'activities'=> ActivityResource::collection($this->activities),
            // 'procedure'=> ProcedureResource::collection($this->procedures),

            'procedure' => ProcedureResource::collection(
                $this->procedures->filter(function ($procedure) {
                    return $procedure->stage_id === $this->stage_id;
                })
            ),
            'sales' => SaleResource::collection($sales),
            'initial_payments' => PaymentPlansResource::collection($initialPayments),
            'balance_payments' => PaymentPlansResource::collection($balancePayments),
            'quotes' =>  QuoteResource::collection($this->quotes),
        ];
    }
}
