<?php
/**
 * BookiFlex Core
 *
 * @package    BookiFlex
 * @subpackage Core
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Core\Admin\CPT;

use BookiFlex\Core\Contract\PluginInterface;
use BookiFlex\Core\Contract\RoleCapabilitiesInterface;

defined( 'ABSPATH' ) || exit;

abstract class CancellationPolicy extends AbstractCPT
{
    public const POST_TYPE              = 'bflex-cancel-policy';
    public const FIELD_PREDEFINED_RULE = 'bflex__cp__predefined_rule';
    public const FIELD_RULES            = 'bflex__cp__rules';
    public const FIELD_CANCELLED_WITHIN_HOURS       = 'cancelled_within_hours';
    public const FIELD_CANCELLED_MIN_HOURS_UNTIL_START       = 'cancelled_min_hours_until_start';
    public const FIELD_CANCELLED_MAX_HOURS_UNTIL_START       = 'cancelled_max_hours_until_start';
    public const FIELD_PENALTY_TYPE        = 'penalty_type';
    public const FIELD_PERCENTAGE_OF_BOOKING_AMOUNT  = 'percentage_amount';
    public const FIELD_FIXED_AMOUNT       = 'fixed_amount';
    public const FIELD_CHARGE_X_NIGHTS = 'charge_x_nights';

    // Predefined rules
    public const RULE_FULLY_FLEXIBLE = 'bflex__cp__rule_fully_flexible';
    public const RULE_NON_REFUNDABLE = 'bflex__cp__rule_non_refundable';
    public const RULE_STRICT = 'bflex__cp__rule_strict';
    public const RULE_FIRST_NIGHT = 'bflex__cp__rule_first_night';
    public const RULE_CUSTOM = 'bflex__cp__rule_custom';

    // Strict policy configuration fields
    public const FIELD_STRICT_TIME_WINDOW = 'bflex__cp__strict_time_window';
    public const FIELD_STRICT_PENALTY_PERCENT = 'bflex__cp__strict_penalty_percent';

    public function register(PluginInterface $plugin): void
    {
        parent::register($plugin);

        $this->customizeExcerpt(
            __('Description', 'bookiflex'),
            __('An optional note for your own reference. This text is not shown to guests.', 'bookiflex')
        );
    }

    public function registerPostType(): void
    {
        $labels = [
            'name'                  => _x('Cancellation Policies', 'Post type general name', 'bookiflex'),
            'singular_name'         => _x('Cancellation Policy', 'Post type singular name', 'bookiflex'),
            'menu_name'             => _x('Cancellation Policies', 'Admin Menu text', 'bookiflex'),
            'name_admin_bar'        => _x('Cancellation Policy', 'Add New on Toolbar', 'bookiflex'),
            'add_new'               => __('Add New', 'bookiflex'),
            'add_new_item'          => __('Add New', 'bookiflex'),
            'new_item'              => __('New Cancellation Policy', 'bookiflex'),
            'edit_item'             => __('Edit Cancellation Policy', 'bookiflex'),
            'view_item'             => __('View Cancellation Policy', 'bookiflex'),
            'all_items'             => __('Cancellation Policies', 'bookiflex'),
            'search_items'          => __('Search', 'bookiflex'),
            'not_found'             => __('Nothing found.', 'bookiflex'),
            'not_found_in_trash'    => __('Nothing found in Trash.', 'bookiflex'),
        ];

        $args = [
            'labels'             => $labels,
            'public'             => true,
            'publicly_queryable' => false,
            'show_ui'            => true,
            'show_in_menu'       => 'bookiflex',
            'query_var'          => true,
            'rewrite'            => ['slug' => self::POST_TYPE],
            'has_archive'        => false,
            'hierarchical'       => false,
            'menu_position'      => 2,
            'supports'           => ['title'],
            'capability_type'    => self::POST_TYPE,
            'capabilities'       => [
                'edit_post'              => RoleCapabilitiesInterface::MANAGE_CONFIGURATION,
                'read_post'              => RoleCapabilitiesInterface::MANAGE_CONFIGURATION,
                'delete_post'            => RoleCapabilitiesInterface::DELETE_CONFIGURATION,
                'edit_posts'             => RoleCapabilitiesInterface::MANAGE_CONFIGURATION,
                'edit_others_posts'      => RoleCapabilitiesInterface::MANAGE_CONFIGURATION,
                'publish_posts'          => RoleCapabilitiesInterface::MANAGE_CONFIGURATION,
                'read_private_posts'     => RoleCapabilitiesInterface::MANAGE_CONFIGURATION,
                'delete_posts'           => RoleCapabilitiesInterface::DELETE_CONFIGURATION,
                'delete_private_posts'   => RoleCapabilitiesInterface::DELETE_CONFIGURATION,
                'delete_published_posts' => RoleCapabilitiesInterface::DELETE_CONFIGURATION,
                'delete_others_posts'    => RoleCapabilitiesInterface::DELETE_CONFIGURATION,
                'edit_private_posts'     => RoleCapabilitiesInterface::MANAGE_CONFIGURATION,
                'edit_published_posts'   => RoleCapabilitiesInterface::MANAGE_CONFIGURATION,
            ],
            'map_meta_cap' => false,
        ];

        register_post_type(self::POST_TYPE, $args);
    }
}
