<?php
/**
 * Class PTFS_Player_Transfers
 * Handles the custom post type registration for Player Transfers.
 */
class PTFS_Player_Transfers {

    /**
     * Initialize hooks and actions for the custom post type.
     */
    public static function init() {
        add_action( 'init', [ __CLASS__, 'register_cpt' ] );
        add_action( 'rwmb_player-transfer-details_after_save_post', [ __CLASS__, 'update_transfer_title' ] );
        // Add 'player_transfer' to the supported SportPress season object types
        add_filter( 'sportspress_season_object_types', function( $object_types ) {
            $object_types[] = 'ptfs_player_transfer';
            return $object_types;
        } );
    }

    /**
     * Register the custom post type for Player Transfers.
     */
    public static function register_cpt() {
        $labels = [
            'name'               => __( 'Player Transfers', 'player-transfers-for-sportspress' ),
            'singular_name'      => __( 'Player Transfer', 'player-transfers-for-sportspress' ),
            'add_new'            => __( 'Add New', 'player-transfers-for-sportspress' ),
            'add_new_item'       => __( 'Add New Transfer', 'player-transfers-for-sportspress' ),
            'edit_item'          => __( 'Edit Transfer', 'player-transfers-for-sportspress' ),
            'new_item'           => __( 'New Transfer', 'player-transfers-for-sportspress' ),
            'all_items'          => __( 'All Transfers', 'player-transfers-for-sportspress' ),
            'menu_name'          => __( 'Player Transfers', 'player-transfers-for-sportspress' ),
        ];

        $args = [
            'labels'             => $labels,
            'public'             => true,
            'has_archive'        => true,
            'menu_icon'          => 'dashicons-randomize',
            'supports'           => false,
            'show_in_rest'       => true,
        ];

        register_post_type( 'ptfs_player_transfer', $args );
    }

    /**
     * Plugin activation callback to flush rewrite rules.
     */
    public static function activate() {
        self::register_cpt();
        flush_rewrite_rules();
    }

    /**
     * Plugin deactivation callback to flush rewrite rules.
     */
    public static function deactivate() {
        flush_rewrite_rules();
    }

    /**
     * Update the post title based on the selected player.
     *
     * @param int     $post_id Post ID.
     */
    public static function update_transfer_title( $post_id ) {
        // Get the player name (assumes it's a post ID of a player).
        $player_name = rwmb_meta( 'ptfs_player_name', '', $post_id );

        if ( empty( $player_name ) ) {
            return;
        }

        // Get the player title.
        $player_title = get_the_title( $player_name );

        if ( empty( $player_title ) ) {
            return;
        }

        // Generate a slug.
        $slug = sanitize_title( $player_title );

        // Prepare update array.
        $player_post = array(
            'ID'         => $post_id,
            'post_title' => $player_title,
            'post_name'  => $slug,
        );
        wp_update_post( $player_post );
    }
}
