<?php
/**
 * Class PTFS_Metaboxes
 * Handles the registration of custom metaboxes using MetaBox.io.
 */
class PTFS_Metaboxes {

    /**
     * Initialize MetaBox hooks.
     */
    public static function init() {
        add_filter( 'rwmb_meta_boxes', [ __CLASS__, 'register_meta_boxes' ] );
        add_filter( 'rwmb_the_value', function( $output, $field, $args, $object_id ) {
            $value = rwmb_meta( 'ptfs_transfer_date', $args, $object_id );
            if( is_admin() && $field['id'] == 'ptfs_transfer_date') {
                $output = wp_date( get_option( 'date_format' ), strtotime( $value ) );
            }
        
            return $output;
        }, 11, 4 );
    }

    /**
     * Register custom meta boxes for the Player Transfers post type.
     *
     * @param array $meta_boxes Existing meta boxes.
     * @return array Modified meta boxes.
     */
    public static function register_meta_boxes( $meta_boxes ) {
        $prefix = 'ptfs_';

        $meta_boxes[] = [
            'title'          => __( 'Player Transfer Details', 'player-transfers-for-sportspress' ),
            'id'             => 'player-transfer-details',
            'post_types'     => ['ptfs_player_transfer'],
            'closed'         => false,
            'default_hidden' => false,
            'autosave'       => false,
            'revision'       => false,
            'fields'         => [
                [
                    'type' => 'custom_html',
                    'std'  => ! ptfs_fs()->is__premium_only() ? sprintf(
                        '<div class="ptfs-premium-notice" style="background: #f8f9fa; padding: 15px; border-left: 4px solid #2271b1; margin-bottom: 15px;">
                            <p style="margin: 0;">%s</p>
                        </div>',
                        sprintf(
                            /* translators: %s: Premium plugin URL */
                            __( 'For extra premium features like automatic player assignments, frontend submissions, more transfer details, etc., upgrade to %s', 'player-transfers-for-sportspress' ),
                            '<a href="edit.php?post_type=ptfs_player_transfer&page=player-transfers-for-sportspress-pricing">Player Transfers for SportsPress Premium</a>'
                        )
                    ) : '',
                ],
                [
                    'type'  => 'heading',
                    'name'  => __( 'Player Details', 'player-transfers-for-sportspress' ),
                    'class' => 'player-transfers-heading',
                ],
                [
                    'name'              => __( 'Player', 'player-transfers-for-sportspress' ),
                    'id'                => $prefix . 'player_name',
                    'type'              => 'post',
                    'post_type'         => ['sp_player'],
                    'field_type'        => 'select_advanced',
                    'add_new'           => false,
                    'multiple'          => false,
                    'parent'            => false,
                    'required'          => true,
                    'clone'             => false,
                    'clone_empty_start' => false,
                    'columns'           => 6,
                ],
                [
                    'name'           => __( 'Season', 'player-transfers-for-sportspress' ),
                    'id'             => $prefix . 'season',
                    'type'           => 'taxonomy',
                    'taxonomy'       => ['sp_season'],
                    'field_type'     => 'select_advanced',
                    'add_new'        => false,
                    'remove_default' => true,
                    'multiple'       => false,
                    'required'       => true,
                    'admin_columns'  => [
                        'position'   => 'after title',
                        'sort'       => true,
                        'searchable' => true,
                        'filterable' => true,
                    ],
                    'columns'        => 3,
                ],
                [
                    'name'              => __( 'League(s)', 'player-transfers-for-sportspress' ),
                    'id'                => $prefix . 'leagues',
                    'type'              => 'taxonomy_advanced',
                    'taxonomy'          => ['sp_league'],
                    'field_type'        => 'select_advanced',
                    'add_new'           => false,
                    'remove_default'    => false,
                    'multiple'          => true,
                    'select_all_none'   => true,
                    'required'          => true,
                    'clone'             => false,
                    'clone_empty_start' => false,
                    'columns'           => 3,
                ],
                [
                    'type' => 'divider',
                ],
                [
                    'type'  => 'heading',
                    'name'  => __( 'Club IN-OUT', 'player-transfers-for-sportspress' ),
                    'class' => 'player-transfers-heading',
                ],
                [
                    'name'              => __( 'Club In', 'player-transfers-for-sportspress' ),
                    'id'                => $prefix . 'club_in',
                    'type'              => 'post',
                    'post_type'         => ['sp_team'],
                    'field_type'        => 'select_advanced',
                    'add_new'           => false,
                    'multiple'          => false,
                    'parent'            => false,
                    'required'          => true,
                    'clone'             => false,
                    'clone_empty_start' => false,
                    'admin_columns'     => [
                        'position'   => 'after ' . $prefix . 'season',
                        'searchable' => true,
                        'filterable' => true,
                    ],
                    'columns'           => 6,
                ],
                [
                    'name'              => __( 'Club Out', 'player-transfers-for-sportspress' ),
                    'id'                => $prefix . 'club_out',
                    'type'              => 'post',
                    'post_type'         => ['sp_team'],
                    'field_type'        => 'select_advanced',
                    'add_new'           => false,
                    'multiple'          => false,
                    'parent'            => false,
                    'required'          => true,
                    'clone'             => false,
                    'clone_empty_start' => false,
                    'admin_columns'     => [
                        'position'   => 'after ' . $prefix . 'club_in',
                        'searchable' => true,
                        'filterable' => true,
                    ],
                    'columns'           => 6,
                ],
                [
                    'type' => 'divider',
                ],
                [
                    'type'  => 'heading',
                    'name'  => __( 'Transfer Details', 'player-transfers-for-sportspress' ),
                    'class' => 'player-transfers-heading',
                ],
                [
                    'name'              => __( 'Transfer Date', 'player-transfers-for-sportspress' ),
                    'id'                => $prefix . 'transfer_date',
                    'type'              => 'date',
                    'timestamp'         => false,
                    'inline'            => false,
                    'required'          => true,
                    'disabled'          => false,
                    'readonly'          => false,
                    'clone'             => false,
                    'clone_empty_start' => false,
                    'admin_columns'     => 'after ' . $prefix . 'club_out',
                    'columns'           => 6,
                ],
            ],
        ];

        return $meta_boxes;
    }
}