<?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\RoleCapabilitiesInterface;
use BookiFlex\Core\Contract\Payment\PaymentGatewayManagerInterface;

defined( 'ABSPATH' ) || exit;

abstract class PaymentType extends AbstractCPT
{
    public function __construct(
        private readonly ?PaymentGatewayManagerInterface $gatewayManager = null,
    )
    {
    }

    public const POST_TYPE                            = 'bflex-payment-type';
    public const FIELD_CALCULATOR_TYPE                = 'bflex__pt__calculator_type';
    public const FIELD_PREPAYMENT_TYPE                = 'bflex__pt__prepayment_type';
    public const FIELD_PREPAYMENT_VALUE               = 'bflex__pt__prepayment_value';
    public const FIELD_PAYMENT_AVAILABILITY_CONDITION = 'bflex__pt__payment_availability_condition';
    public const FIELD_PAYMENT_AVAILABILITY_VALUE     = 'bflex__pt__payment_availability_value';
    public const FIELD_PAYMENT_AVAILABILITY_PERIOD    = 'bflex__pt__payment_availability_period';
    public const FIELD_PAYMENT_PAYABLE                = 'bflex__pt__payable';
    public const FIELD_PAYMENT_AUTHORIZATION          = 'bflex__pt__payment_authorization';
    public const FIELD_PAYMENT_SYSTEM                 = 'bflex__pt__payment_system';
    public const FIELD_TIME_FOR_CONFIRMATION          = 'bflex__pt__time_for_confirmation';


    public function registerPostType(): void
    {
        $labels = [
            'name'                  => _x('Payment Types', 'Post type general name', 'bookiflex'),
            'singular_name'         => _x('Payment Type', 'Post type singular name', 'bookiflex'),
            'menu_name'             => _x('Payment Types', 'Admin Menu text', 'bookiflex'),
            'name_admin_bar'        => _x('Payment Type', 'Add New on Toolbar', 'bookiflex'),
            'add_new'               => __('Add New', 'bookiflex'),
            'add_new_item'          => __('Add New', 'bookiflex'),
            'new_item'              => __('New Payment Type', 'bookiflex'),
            'edit_item'             => __('Edit Payment Type', 'bookiflex'),
            'view_item'             => __('View Payment Type', 'bookiflex'),
            'all_items'             => __('Payment Types', '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'      => 3,
            '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);
    }

    protected function getPaymentGateways(): array
    {
        $gateways = $this->gatewayManager?->getPaymentGateways() ?? [];

        $prepared = [];
        foreach ($gateways as $gateway) {
            $prepared[$gateway['id']] = $gateway;
        }

        return $prepared;
    }
}
