<?php

/**
 * BookiFlex
 *
 * @package   BookiFlex
 * @author    BookiFlex
 * @copyright Copyright (c) 2025 BookiFlex
 * @license   GPL-2.0-or-later
 *
 * @link      https://bookiflex.com
 */
declare (strict_types = 1);
namespace BookiFlex\Plugin\Admin\CPT;

use BookiFlex\Core\Admin\CPT\PaymentType as BaseCPT;
use BookiFlex\Core\Admin\Field\RadioCard;
use BookiFlex\Core\Admin\Field\SectionNotice;
use BookiFlex\Core\Contract\PluginInterface;
use BookiFlex\Core\Model\PaymentType\PaymentTypeInterface;
use BookiFlex\Core\Util\ProFeature;
use Carbon_Fields\Container;
use Carbon_Fields\Field;
defined( 'ABSPATH' ) || exit;
class PaymentType extends BaseCPT {
    public function register( PluginInterface $plugin ) : void {
        parent::register( $plugin );
        $this->customizeExcerpt( __( 'Description', 'bookiflex' ), __( 'A brief description of this payment type. This text may be shown to guests during the booking process.', 'bookiflex' ) );
    }

    public function registerMetaBoxes() : void {
        $paymentGateways = $this->getPaymentGateways();
        // Build payment settings fields
        $paymentSettingsFields = [
            SectionNotice::make( 'payment_type_notice', __( 'Choose when the booking gets confirmed. If payment is required, the booking is held until paid — if the deadline passes without payment, the booking moves to <strong>Overdue</strong> and the manager decides whether to cancel it or extend the deadline. If no payment is required, the booking is either confirmed immediately or placed in <strong>Waiting confirmation</strong>, depending on your Auto Confirmation settings.', 'bookiflex' ) ),
            ...RadioCard::make(
                self::FIELD_CALCULATOR_TYPE,
                __( 'How will the payment be collected?', 'bookiflex' ),
                [
                    PaymentTypeInterface::PAYMENT_ON_ARRIVAL_TYPE   => [
                        'title'       => __( 'Guest pays on arrival', 'bookiflex' ),
                        'description' => __( 'Booking is confirmed immediately. No online payment required.', 'bookiflex' ),
                    ],
                    PaymentTypeInterface::BANK_PAYMENT_TYPE         => [
                        'title'       => __( 'Bank transfer (no payment required)', 'bookiflex' ),
                        'description' => __( 'Booking is confirmed immediately. Bank details are shared with the guest.', 'bookiflex' ),
                    ],
                    PaymentTypeInterface::BANK_PAYMENT_PAYABLE_TYPE => [
                        'title'       => __( 'Bank transfer (payment required)', 'bookiflex' ),
                        'description' => __( 'Booking is held until the guest completes the bank transfer within the allowed time.', 'bookiflex' ),
                    ],
                    PaymentTypeInterface::INTERNET_PAYMENT_TYPE     => [
                        'title'       => __( 'Online payment', 'bookiflex' ),
                        'description' => __( 'Booking is held until the guest pays online via the selected payment gateway.', 'bookiflex' ),
                    ],
                ],
                PaymentTypeInterface::PAYMENT_ON_ARRIVAL_TYPE
            ),
            // Payment Gateway Settings
            Field::make( 'separator', 'bflex_payment_gateway_separator', __( 'Payment Gateway Settings', 'bookiflex' ) )->set_conditional_logic( [[
                'field'   => self::FIELD_CALCULATOR_TYPE,
                'value'   => [PaymentTypeInterface::BANK_PAYMENT_PAYABLE_TYPE, PaymentTypeInterface::INTERNET_PAYMENT_TYPE],
                'compare' => 'IN',
            ]] ),
            Field::make( 'select', self::FIELD_PAYMENT_SYSTEM, __( 'Payment gateway', 'bookiflex' ) )->set_width( 50 )->set_options( array_map( fn( $gateway ) => $gateway['name'], $paymentGateways ) )->set_conditional_logic( [[
                'field'   => self::FIELD_CALCULATOR_TYPE,
                'value'   => [PaymentTypeInterface::BANK_PAYMENT_PAYABLE_TYPE, PaymentTypeInterface::INTERNET_PAYMENT_TYPE],
                'compare' => 'IN',
            ]] )->set_help_text( __( 'Choose the system used to process the payment. For example, Stripe for online payments or "Offline payment" for manual processing (e.g. bank transfer).', 'bookiflex' ) ),
        ];
        // Continue with confirmation and prepayment fields
        $paymentSettingsFields = array_merge( $paymentSettingsFields, [
            /**
             * Confirmation Rule
             */
            Field::make( 'separator', 'bflex_confirmation_separator', __( 'Confirmation rule', 'bookiflex' ) )->set_conditional_logic( [[
                'field'   => self::FIELD_CALCULATOR_TYPE,
                'value'   => [PaymentTypeInterface::BANK_PAYMENT_PAYABLE_TYPE, PaymentTypeInterface::INTERNET_PAYMENT_TYPE],
                'compare' => 'IN',
            ]] ),
            Field::make( 'text', self::FIELD_TIME_FOR_CONFIRMATION, __( 'Confirmation timeout (in hours)', 'bookiflex' ) )->set_width( 50 )->set_attribute( 'type', 'number' )->set_conditional_logic( [[
                'field'   => self::FIELD_CALCULATOR_TYPE,
                'value'   => [PaymentTypeInterface::BANK_PAYMENT_PAYABLE_TYPE, PaymentTypeInterface::INTERNET_PAYMENT_TYPE],
                'compare' => 'IN',
            ]] )->set_help_text( __( 'Set the time allowed for the guest to complete payment. If the payment isn\'t received in time, the booking will be marked as overdue (the availability is not restored).', 'bookiflex' ) ),
            // Prepayment rule
            Field::make( 'separator', 'bflex_prepayment_separator', __( 'Prepayment rule', 'bookiflex' ) )->set_conditional_logic( [[
                'field'   => self::FIELD_CALCULATOR_TYPE,
                'value'   => [PaymentTypeInterface::BANK_PAYMENT_PAYABLE_TYPE, PaymentTypeInterface::INTERNET_PAYMENT_TYPE],
                'compare' => 'IN',
            ]] ),
        ] );
        $paymentSettingsFields = array_merge( $paymentSettingsFields, $this->addBasePaymentSettingsFields() );
        Container::make( 'post_meta', __( 'Configuration', 'bookiflex' ) )->where( 'post_type', '=', self::POST_TYPE )->add_fields( $paymentSettingsFields );
    }

    private function addBasePaymentSettingsFields() : array {
        $paymentSettingsFields[] = Field::make( 'select', self::FIELD_PREPAYMENT_VALUE, __( 'Percentage of booking amount', 'bookiflex' ) )->set_conditional_logic( [[
            'field'   => self::FIELD_CALCULATOR_TYPE,
            'value'   => [PaymentTypeInterface::BANK_PAYMENT_PAYABLE_TYPE, PaymentTypeInterface::INTERNET_PAYMENT_TYPE],
            'compare' => 'IN',
        ]] )->set_options( [
            20  => '20%',
            50  => '50%',
            100 => '100%',
        ] )->set_help_text( __( 'Define the required prepayment. For example, 20%.', 'bookiflex' ) );
        $paymentSettingsFields[] = ProFeature::notice( 'pro_notice_payment_features', __( 'BookiFlex Pro offers more flexibility: set <strong>custom prepayment amounts</strong> or base it on <strong>X nights</strong>. Plus, <strong>control when payment options are available</strong> — for example, disable bank transfers for last-minute bookings.', 'bookiflex' ) );
        return $paymentSettingsFields;
    }

}
