<?php

namespace App\Modules\VisaMasterPaymentOrder\Repositories;

use App\Modules\HalkbankOnlinePayment\Repositories\HalkbankOnlinePaymentRepository;
use App\Modules\Makeable;
use App\Modules\OnlinePayment\Repositories\OnlinePaymentRepository;
use App\Modules\VisaMasterPaymentOrder\Models\VisaMasterPaymentOrder;
use App\Modules\VisaMasterPaymentOrder\Models\VisaMasterPaymentOrderItem;
use Illuminate\Support\Carbon;

class VisaMasterPaymentOrderRepository
{
    use Makeable;

    /**
     * Bank fee
     */
    public int|float $bankFee = 20;

    /**
     * Gbus fee
     */
    public int|float $gbusFee = 3;

    /**
     * Bank fee
     */
    public function bankFee(): int|float
    {
        return $this->bankFee;
    }

    /**
     * Gbus fee
     */
    public function gbusFee(): int|float
    {
        return $this->gbusFee;
    }

    /**
     * Has been paid this month
     */
    public function hasBeenPaidThisMonth(VisaMasterPaymentOrder $record, Carbon $today): bool
    {
        return $record->paymentItems->contains(
            fn (VisaMasterPaymentOrderItem $item): bool => $item->paid && $item->created_at->format('m Y') === $today->format('m Y')
        );
    }

    /**
     * Is end of month
     */
    public function isEndOfMonth(Carbon $today): bool
    {
        $lastDay = $today->copy()->endOfMonth();
        $lastDayOfWeek = $lastDay->format('l');

        $oneDayBefore = $lastDay->copy()->subDay();          // Saturday...
        $twoDaysBefore = $lastDay->copy()->subDays(2);       // Friday...

        // Condition 1: Today is the last day
        if ($today->isSameDay($lastDay)) {
            return true;
        }

        // Condition 2: Last day is Sunday → forbid Friday & Saturday
        if ($lastDayOfWeek === 'Sunday' && ($today->isSameDay($oneDayBefore) || $today->isSameDay($twoDaysBefore))) {
            return true;
        }

        // Condition 3: Last day is Saturday → forbid Friday
        if ($lastDayOfWeek === 'Saturday' && $today->isSameDay($oneDayBefore)) {
            return true;
        }

        // Default: allow payment
        return false;
    }

    /**
     * Create online payment order
     */
    public function createOnlinePaymentOrder(VisaMasterPaymentOrder $record, int|float|string $amount): OnlinePaymentRepository
    {
        /** @var \App\Modules\Branch\Models\Branch */
        $branch = $record->branch;

        return OnlinePaymentRepository::make(relatedModel: $record)
            ->setPaymentProvider(
                HalkbankOnlinePaymentRepository::make(
                    username: $branch->billing_visa_master_username,
                    password: $branch->billing_visa_master_password,
                    amount: $amount,
                    returnUrl: route('halkbank-online-payment.store'),
                    description: 'Visa/Master tölegi'
                )
            )
            ->sendRequest();
    }

    /**
     * Create payment record
     */
    public function createPaymentRecord(VisaMasterPaymentOrder $record, OnlinePaymentRepository $onlinePaymentRepository, int|string|float $usdRate): void
    {
        VisaMasterPaymentOrderItem::create([
            'visa_master_payment_order_id' => $record->id,
            'online_payment_id' => $onlinePaymentRepository->onlinePayment()->id,
            'payment_order_number' => $onlinePaymentRepository->onlinePayment()->orderNumber,
            'tmt_payment_amount' => $onlinePaymentRepository->paymentProvider()->amount(),
            'usd_payment_amount' => $usdRate,
        ]);
    }
}
