<?php

namespace BWDK\Core;

use BWDK\Admin\Assets as AdminAssets;
use BWDK\Admin\AdminOrderHooks;
use BWDK\Admin\SettingsPage;
use BWDK\API\AjaxHandler;
use BWDK\Front\Assets as FrontAssets;
use BWDK\API\DigiExpress_Webhook;
use BWDK\Integrations\Digify\Routes\CallbackRoute;
use BWDK\Support\Constant;
use BWDK\Woo\Order\BulkVerifyBwdkOrders;
use BWDK\Woo\Order\OrderCancelSync;
use BWDK\Woo\Order\OrderListColumns;
use BWDK\Woo\Order\OrderMetabox;
use BWDK\Woo\Order\OrderStatusGuard;
use BWDK\Woo\SingleBuyEndpoint;
use BWDK\Woo\WooHooks;

class ServiceProvider
{
    public function register(): void
    {
        // غیرفعال‌سازی ایمیل خوشامد برای کاربران خودکار
        add_filter('woocommerce_email_enabled_customer_new_account', function ($enabled, $email) {
            if (!isset($email->object) || !($email->object instanceof \WP_User)) {
                return $enabled;
            }
            $user_id = $email->object->ID ?? 0;
            if ($user_id && get_user_meta($user_id, Constant::META_AUTO_CREATED, true)) {
                return false;
            }
            return $enabled;
        }, 10, 2);

        App::bind(AjaxHandler::class, new AjaxHandler());
        App::get(AjaxHandler::class)->register();

        App::bind(CallbackRoute::class, new CallbackRoute());
        App::get(CallbackRoute::class)->register();

        App::bind(SingleBuyEndpoint::class, new SingleBuyEndpoint());
        App::get(SingleBuyEndpoint::class)->register();

        App::bind(DigiExpress_Webhook::class, new DigiExpress_Webhook());
        App::get(DigiExpress_Webhook::class)->register();

        // Register order cancellation sync handler (works in both admin and frontend)
        App::bind(OrderCancelSync::class, new OrderCancelSync());
        App::get(OrderCancelSync::class)->register();

        // Guard against status downgrade back to processing
        App::bind(OrderStatusGuard::class, new OrderStatusGuard());
        App::get(OrderStatusGuard::class)->register();

        if (is_admin()) {

            App::bind(SettingsPage::class, new SettingsPage());
            App::bind(AdminAssets::class, new AdminAssets());

            App::get(SettingsPage::class)->register();
            App::get(AdminAssets::class)->register();


            App::bind(OrderListColumns::class, new OrderListColumns());
            App::get(OrderListColumns::class)->register();

            App::bind(OrderMetabox::class, new OrderMetabox());
            App::get(OrderMetabox::class)->register();

            App::bind(BulkVerifyBwdkOrders::class, new BulkVerifyBwdkOrders());
            App::get(BulkVerifyBwdkOrders::class)->register();

            // Admin-only: shipping promotion display in order edit screen (UI-only).
            App::bind(AdminOrderHooks::class, new AdminOrderHooks());
            App::get(AdminOrderHooks::class)->register();





        } else {


            App::bind(FrontAssets::class, new FrontAssets());
            App::get(FrontAssets::class)->register();



            App::bind(WooHooks::class, new WooHooks());
            App::get(WooHooks::class)->register();





        }

    }
}
