<?php

namespace WooNooW\Api;

use WP_REST_Request;
use WP_REST_Response;
use WooNooW\Api\CheckoutController;
use WooNooW\Api\OrdersController;
use WooNooW\Api\AnalyticsController;
use WooNooW\Api\AuthController;
use WooNooW\Api\PaymentsController;
use WooNooW\Api\StoreController;
use WooNooW\Api\ShippingController;
use WooNooW\Api\TaxController;
use WooNooW\Api\PickupLocationsController;
use WooNooW\Api\EmailController;
use WooNooW\Api\DeveloperController;
use WooNooW\Api\SystemController;
use WooNooW\Api\NotificationsController;
use WooNooW\Api\ActivityLogController;
use WooNooW\Api\ProductsController;
use WooNooW\Api\CouponsController;
use WooNooW\Api\CustomersController;
use WooNooW\Api\NewsletterController;
use WooNooW\Api\ModulesController;
use WooNooW\Api\ModuleSettingsController;
use WooNooW\Api\CampaignsController;
use WooNooW\Api\DocsController;
use WooNooW\Api\LicensesController;
use WooNooW\Api\SubscriptionsController;
use WooNooW\Api\SoftwareController;
use WooNooW\Frontend\ShopController;
use WooNooW\Frontend\CartController as FrontendCartController;
use WooNooW\Frontend\AccountController;
use WooNooW\Frontend\AddressController;
use WooNooW\Frontend\WishlistController;
use WooNooW\Frontend\HookBridge;
use WooNooW\Api\Controllers\SettingsController;
use WooNooW\Api\Controllers\CartController as ApiCartController;
use WooNooW\Admin\AppearanceController;
use WooNooW\Api\PagesController;
use WooNooW\Api\OnboardingController;

class Routes
{
    public static function init()
    {
        // Initialize controllers (register action hooks)
        OrdersController::init();
        AppearanceController::init();

        // Initialize CartController auth bypass (must be before rest_api_init)
        FrontendCartController::init();

        add_action('rest_api_init', function () {
            $namespace = 'woonoow/v1';

            // Auth endpoints (public - no permission check)
            register_rest_route($namespace, '/auth/login', [
                'methods'             => 'POST',
                'callback'            => [AuthController::class, 'login'],
                'permission_callback' => '__return_true',
            ]);

            register_rest_route($namespace, '/auth/logout', [
                'methods'             => 'POST',
                'callback'            => [AuthController::class, 'logout'],
                'permission_callback' => '__return_true',
            ]);

            register_rest_route($namespace, '/auth/check', [
                'methods'             => 'GET',
                'callback'            => [AuthController::class, 'check'],
                'permission_callback' => '__return_true',
            ]);

            // Customer login endpoint (no admin permission required)
            register_rest_route($namespace, '/auth/customer-login', [
                'methods'             => 'POST',
                'callback'            => [AuthController::class, 'customer_login'],
                'permission_callback' => '__return_true',
            ]);

            // Forgot password endpoint (public)
            register_rest_route($namespace, '/auth/forgot-password', [
                'methods'             => 'POST',
                'callback'            => [AuthController::class, 'forgot_password'],
                'permission_callback' => '__return_true',
            ]);

            // Validate password reset key (public)
            register_rest_route($namespace, '/auth/validate-reset-key', [
                'methods'             => 'POST',
                'callback'            => [AuthController::class, 'validate_reset_key'],
                'permission_callback' => '__return_true',
            ]);

            // Reset password with key (public)
            register_rest_route($namespace, '/auth/reset-password', [
                'methods'             => 'POST',
                'callback'            => [AuthController::class, 'reset_password'],
                'permission_callback' => '__return_true',
            ]);

            // Defer to controllers to register their endpoints
            CheckoutController::register();
            OrdersController::register();
            AnalyticsController::register_routes();

            // Settings controller
            $settings_controller = new SettingsController();
            $settings_controller->register_routes();

            // Payments controller
            $payments_controller = new PaymentsController();
            $payments_controller->register_routes();

            // Store controller
            $store_controller = new StoreController();
            $store_controller->register_routes();

            // Shipping controller
            $shipping_controller = new ShippingController();
            $shipping_controller->register_routes();

            // Tax controller
            $tax_controller = new TaxController();
            $tax_controller->register_routes();

            // Pickup locations controller
            $pickup_controller = new PickupLocationsController();
            $pickup_controller->register_routes();

            // Email controller
            $email_controller = new EmailController();
            $email_controller->register_routes();

            // Developer controller
            $developer_controller = new DeveloperController();
            $developer_controller->register_routes();

            // System controller
            $system_controller = new SystemController();
            $system_controller->register_routes();

            // Notifications controller
            $notifications_controller = new NotificationsController();
            $notifications_controller->register_routes();

            // Activity Log controller
            $activity_log_controller = new ActivityLogController();
            $activity_log_controller->register_routes();

            // Products controller
            ProductsController::register_routes();

            // Coupons controller
            CouponsController::register_routes();

            // Customers controller
            CustomersController::register_routes();

            // Newsletter controller
            NewsletterController::register_routes();

            // Campaigns controller
            CampaignsController::register_routes();

            // Licenses controller (licensing module)
            LicensesController::register_routes();

            // Subscriptions controller (subscription module)
            SubscriptionsController::register_routes();

            // Software controller (software distribution module)
            SoftwareController::register_routes();

            // Modules controller
            $modules_controller = new ModulesController();
            $modules_controller->register_routes();

            // Module Settings controller
            $module_settings_controller = new ModuleSettingsController();
            $module_settings_controller->register_routes();

            // Documentation controller
            $docs_controller = new DocsController();
            $docs_controller->register_routes();

            // Onboarding controller
            $onboarding_controller = new OnboardingController();
            $onboarding_controller->register_routes();

            // Affiliate Admin controller
            $affiliate_admin_controller = new \WooNooW\Api\Controllers\AffiliateAdminController();
            $affiliate_admin_controller->register_routes();

            // Affiliate Customer controller
            $affiliate_customer_controller = new \WooNooW\Api\Controllers\AffiliateCustomerController();
            $affiliate_customer_controller->register_routes();

            // Frontend controllers (customer-facing)
            ShopController::register_routes();
            FrontendCartController::register_routes();
            AccountController::register_routes();
            AddressController::register_routes();
            WishlistController::register_routes();
            HookBridge::register_routes();

            // Pages and templates controller
            PagesController::register_routes();
        });
    }
}
