<?php
/**
 * Order Status — registers 4 custom WC statuses for returns workflow.
 *
 * @package WPNest_Returns_For_WooCommerce
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class WPNest_RFW_Order_Status {

    private static $instance = null;

    public static function get_instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        add_action( 'init', array( __CLASS__, 'register' ), 11 );
        add_filter( 'wc_order_statuses', array( __CLASS__, 'add_to_wc_statuses' ) );
        add_filter( 'woocommerce_register_shop_order_statuses', array( __CLASS__, 'register_shop_order_statuses' ) );
        add_filter( 'bulk_actions-edit-shop_order', array( __CLASS__, 'add_bulk_actions' ) );
        add_filter( 'bulk_actions-woocommerce_page_wc-orders', array( __CLASS__, 'add_bulk_actions' ) );
        add_action( 'admin_head', array( __CLASS__, 'admin_status_styles' ) );
    }

    /**
     * Returns map of status key (with wc- prefix) => translated label.
     *
     * @return array
     */
    public static function get_statuses() {
        $defs = self::get_status_defs();
        $out  = array();
        foreach ( $defs as $key => $def ) {
            $out[ $key ] = $def['label'];
        }
        return $out;
    }

    /**
     * Per-status registration data. Literal strings are required by Plugin Check
     * for _n_noop(), so each status has its own hardcoded `label_count` call.
     */
    private static function get_status_defs() {
        return array(
            'wc-rfw-pending'  => array(
                'label'       => _x( 'Return Pending', 'Order status', 'wpnest-returns-for-woocommerce' ),
                /* translators: %s: number of orders */
                'label_count' => _n_noop( 'Return Pending <span class="count">(%s)</span>', 'Return Pending <span class="count">(%s)</span>', 'wpnest-returns-for-woocommerce' ),
            ),
            'wc-rfw-accepted' => array(
                'label'       => _x( 'Return Accepted', 'Order status', 'wpnest-returns-for-woocommerce' ),
                /* translators: %s: number of orders */
                'label_count' => _n_noop( 'Return Accepted <span class="count">(%s)</span>', 'Return Accepted <span class="count">(%s)</span>', 'wpnest-returns-for-woocommerce' ),
            ),
            'wc-rfw-rejected' => array(
                'label'       => _x( 'Return Rejected', 'Order status', 'wpnest-returns-for-woocommerce' ),
                /* translators: %s: number of orders */
                'label_count' => _n_noop( 'Return Rejected <span class="count">(%s)</span>', 'Return Rejected <span class="count">(%s)</span>', 'wpnest-returns-for-woocommerce' ),
            ),
            'wc-rfw-refunded' => array(
                'label'       => _x( 'Return Refunded', 'Order status', 'wpnest-returns-for-woocommerce' ),
                /* translators: %s: number of orders */
                'label_count' => _n_noop( 'Return Refunded <span class="count">(%s)</span>', 'Return Refunded <span class="count">(%s)</span>', 'wpnest-returns-for-woocommerce' ),
            ),
        );
    }

    public static function register() {
        foreach ( self::get_status_defs() as $key => $def ) {
            register_post_status(
                $key,
                array(
                    'label'                     => $def['label'],
                    'public'                    => false,
                    'exclude_from_search'       => false,
                    'show_in_admin_all_list'    => true,
                    'show_in_admin_status_list' => true,
                    'label_count'               => $def['label_count'],
                )
            );
        }
    }

    public static function add_to_wc_statuses( $statuses ) {
        return array_merge( $statuses, self::get_statuses() );
    }

    public static function register_shop_order_statuses( $order_statuses ) {
        foreach ( self::get_status_defs() as $key => $def ) {
            $order_statuses[ $key ] = array(
                'label'                     => $def['label'],
                'public'                    => false,
                'exclude_from_search'       => false,
                'show_in_admin_all_list'    => true,
                'show_in_admin_status_list' => true,
                'label_count'               => $def['label_count'],
            );
        }
        return $order_statuses;
    }

    public static function add_bulk_actions( $actions ) {
        $actions['mark_rfw-pending']  = __( 'Change status to Return Pending', 'wpnest-returns-for-woocommerce' );
        $actions['mark_rfw-accepted'] = __( 'Change status to Return Accepted', 'wpnest-returns-for-woocommerce' );
        $actions['mark_rfw-rejected'] = __( 'Change status to Return Rejected', 'wpnest-returns-for-woocommerce' );
        $actions['mark_rfw-refunded'] = __( 'Change status to Return Refunded', 'wpnest-returns-for-woocommerce' );
        return $actions;
    }

    /**
     * Color-code custom statuses in WC admin order list — matches PH Free design tokens.
     */
    public static function admin_status_styles() {
        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
        if ( ! $screen ) {
            return;
        }
        $allowed = array( 'edit-shop_order', 'woocommerce_page_wc-orders', 'shop_order' );
        if ( ! in_array( $screen->id, $allowed, true ) ) {
            return;
        }
        ?>
        <style>
            .order-status.status-rfw-pending { background:#fff3cd; color:#856404; }
            .order-status.status-rfw-accepted { background:#cce5ff; color:#004085; }
            .order-status.status-rfw-rejected { background:#f8d7da; color:#721c24; }
            .order-status.status-rfw-refunded { background:#d4edda; color:#155724; }
        </style>
        <?php
    }
}
