<?php

namespace App\Services\Property\Financing;

use App\Enums\PaymentMethod;
use App\Http\Messages\ErrorMessages;
use App\Repositories\Property\Financing\FinancingRepositoryInterface;
use App\Repositories\Property\Financing\PaymentPlansRepositoryInterface;

class FinancingService
{
    protected $financingRepository;
    protected $PaymentPlansRepository;

    public function __construct(
        FinancingRepositoryInterface $financingRepository,
        PaymentPlansRepositoryInterface $PaymentPlansRepository
    ) {
        $this->financingRepository = $financingRepository;
        $this->PaymentPlansRepository = $PaymentPlansRepository;
    }

    public function createFinancingProperty($data, $propertyId)
    {
        $financing = $this->createFinancing($data, $propertyId);
        if (isset($data['payment_plans'])) {
            $this->createPaymentPlans($data['payment_plans'], $financing->id);
        }
        return $financing;
    }

    public function createFinancing($data, $propertyId)
    {
        $data['price'] = isset($data['price']) ? (float)$data['price'] : 0.0;
        $data['percentage_initial_fee'] = isset($data['percentage_initial_fee']) ? $data['percentage_initial_fee'] : 0.0;
        $data['initial_fee'] = $this->cuota_Inicial($data['price'],  $data['percentage_initial_fee']);

        if ($data['payment_method'] = PaymentMethod::CASH->name) {
            $data['years_term'] = 0;
            $data['interest'] = 0;
        }
        $data['property_id'] = $propertyId;
        // dd($data);
        return $this->financingRepository->create($data);
    }

    public function createPaymentPlans($data, $financingId)
    {
        foreach ($data as $differData) {
            $differData['financing_id'] = $financingId;
            $this->PaymentPlansRepository->create($differData);
        }
    }

    public function cuota_Inicial($price, $percentage_initial): float
    {
        return  $price * ($percentage_initial  / 100);
    }

    public function UpdatePropertyFinancing($idFinancing, $data)
    {
        $data['price'] = isset($data['price']) ? (float)$data['price'] : 0.0;
        $data['percentage_initial_fee'] = isset($data['percentage_initial_fee']) ? (float)$data['percentage_initial_fee'] : 0.0;
        $ $data['initial_fee'] = $this->cuota_Inicial($data['price'],  $data['initial_fee']);

        if ($data['payment_method'] = PaymentMethod::CASH->name) {
            $data['years_term'] = 0;
            $data['interest'] = 0;
        }
        $financing = $this->financingRepository->update($idFinancing, $data);
        if (isset($data['paymentPlans'])) {
            $this->updatePaymentPlans($data['paymentPlans'], $financing->id);
        }
        return $financing;
    }

    public function updatePaymentPlans($data, $financingId)
    {
        foreach ($data as $differData) {
            $differData['financing_id'] = $financingId;
            if (isset($differData['id'])) {
                // Intentamos encontrar el plan de pago
                $existingPlan = $this->PaymentPlansRepository->find($differData['id']);
                if ($existingPlan) {
                    // Si existe, lo actualizamos
                    $this->PaymentPlansRepository->update($differData['id'], $differData);
                } else {
                    // Si no existe, podrías crearlo si es necesario
                    $this->PaymentPlansRepository->create($differData);
                }
            } else {
                // Si no se provee un ID, creamos un nuevo plan de pago
                $this->PaymentPlansRepository->create($differData);
            }
        }
    }

    public function getFinancingByPropertyId($propertyId)
    {

        $financings =  $this->financingRepository->findByProperty($propertyId);
        if ($financings->isEmpty()) {
            throw new \Exception(ErrorMessages::FINANCING_NOT_FOUND, 404);
        }
        return $financings;
    }

    public function getFinancingByPropertyIdByMethod($propertyId, $method)
    {
        $financing =  $this->financingRepository->findByPropertyMethod($propertyId, $method);
        if (!$financing) {
            throw new \Exception(ErrorMessages::FINANCING_NOT_FOUND, 404);
        }
        return $financing;
    }
}
