<?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\CancellationPolicy as BaseCPT;
use BookiFlex\Core\Contract\CancellationPolicy\CancellationPolicyTypeInterface;
use BookiFlex\Core\Util\ProFeature;
use BookiFlex\Core\Admin\Field\RadioCard;
use BookiFlex\Core\Admin\Field\SectionNotice;
use Carbon_Fields\Container;
use Carbon_Fields\Field;
defined( 'ABSPATH' ) || exit;
class CancellationPolicy extends BaseCPT {
    public function registerMetaBoxes() : void {
        // Build policy options with descriptions for radio cards
        $policyOptions = [
            self::RULE_FULLY_FLEXIBLE => [
                'title'       => __( 'Fully flexible', 'bookiflex' ),
                'description' => __( 'Free cancellation at any time, no penalty will be charged.', 'bookiflex' ),
            ],
            self::RULE_STRICT         => [
                'title'       => __( 'Strict', 'bookiflex' ),
                'description' => __( 'Configurable cancellation with custom time window and penalty percentage.', 'bookiflex' ),
            ],
            self::RULE_FIRST_NIGHT    => [
                'title'       => __( 'First night penalty', 'bookiflex' ),
                'description' => __( 'Cancellation at any time will incur a penalty equal to the first night\'s stay.', 'bookiflex' ),
            ],
            self::RULE_NON_REFUNDABLE => [
                'title'       => __( 'Non-refundable', 'bookiflex' ),
                'description' => __( 'Cancellation at any time will incur a 100% charge of the booking amount.', 'bookiflex' ),
            ],
        ];
        $fields = [
            SectionNotice::make( 'cancellation_policy_html', __( 'A cancellation policy defines how and when cancellation penalties apply. Later, this policy can be assigned to a rate plan.', 'bookiflex' ) ),
            ...RadioCard::make(
                self::FIELD_PREDEFINED_RULE,
                __( 'Rule', 'bookiflex' ),
                $policyOptions,
                self::RULE_FULLY_FLEXIBLE
            ),
            Field::make( 'separator', 'bflex__cp__strict_separator', __( 'Configuration', 'bookiflex' ) )->set_conditional_logic( [[
                'field' => self::FIELD_PREDEFINED_RULE,
                'value' => self::RULE_STRICT,
            ]] ),
            Field::make( 'select', self::FIELD_STRICT_TIME_WINDOW, __( 'Cancellation window', 'bookiflex' ) )->set_conditional_logic( [[
                'field' => self::FIELD_PREDEFINED_RULE,
                'value' => self::RULE_STRICT,
            ]] )->set_options( [
                '24'  => __( '24 hours', 'bookiflex' ),
                '48'  => __( '48 hours', 'bookiflex' ),
                '72'  => __( '72 hours', 'bookiflex' ),
                '168' => __( '7 days', 'bookiflex' ),
            ] )->set_default_value( '168' )->set_help_text( __( 'Time window before check-in when penalty applies', 'bookiflex' ) )->set_width( 50 ),
            Field::make( 'select', self::FIELD_STRICT_PENALTY_PERCENT, __( 'Penalty amount', 'bookiflex' ) )->set_conditional_logic( [[
                'field' => self::FIELD_PREDEFINED_RULE,
                'value' => self::RULE_STRICT,
            ]] )->set_options( [
                '20'  => __( '20%', 'bookiflex' ),
                '50'  => __( '50%', 'bookiflex' ),
                '100' => __( '100%', 'bookiflex' ),
            ] )->set_default_value( '100' )->set_help_text( __( 'Penalty percentage of booking amount', 'bookiflex' ) )->set_width( 50 )
        ];
        $fields = array_merge( $fields, $this->addNotice() );
        Container::make( 'post_meta', __( 'Cancellation rules', 'bookiflex' ) )->where( 'post_type', '=', self::POST_TYPE )->add_fields( $fields );
    }

    private function addNotice() : array {
        return [ProFeature::notice( 'pro_notice_custom_policy', __( 'Create a <strong>Custom cancellation policy</strong> with multiple levels and flexible rules tailored to your property.', 'bookiflex' ) )];
    }

}
