<?php
/**
 * Platform_Shop_Controller — فروش پلتفرم ACC Shop به فروشگاه‌های دیگر
 */
if ( ! defined( 'ABSPATH' ) ) exit;

require_once ACC_PATH . 'includes/helpers/acc-shop-platform.php';

class Platform_Shop_Controller {

    public static function register_routes(): void {
        $ns = 'acc/v1';

        register_rest_route( $ns, '/public/platform/features', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'features' ], 'permission_callback' => '__return_true',
        ] );
        register_rest_route( $ns, '/public/platform/stats', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'stats' ], 'permission_callback' => '__return_true',
        ] );
        register_rest_route( $ns, '/public/platform/demo-request', [
            'methods' => 'POST', 'callback' => [ __CLASS__, 'demo_request' ], 'permission_callback' => '__return_true',
        ] );
        register_rest_route( $ns, '/public/platform/docs', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'docs' ], 'permission_callback' => '__return_true',
        ] );

        register_rest_route( $ns, '/arag/admin/branding', [
            [ 'methods' => 'GET', 'callback' => [ __CLASS__, 'get_branding' ], 'permission_callback' => [ 'Arag_Shop_Controller', 'admin_perm' ] ],
            [ 'methods' => 'PUT', 'callback' => [ __CLASS__, 'save_branding' ], 'permission_callback' => [ 'Arag_Shop_Controller', 'admin_perm' ] ],
        ] );
        register_rest_route( $ns, '/arag/admin/developer/keys', [
            [ 'methods' => 'GET', 'callback' => [ __CLASS__, 'list_keys' ], 'permission_callback' => [ 'Arag_Shop_Controller', 'admin_perm' ] ],
            [ 'methods' => 'POST', 'callback' => [ __CLASS__, 'create_key' ], 'permission_callback' => [ 'Arag_Shop_Controller', 'admin_perm' ] ],
        ] );
        register_rest_route( $ns, '/arag/admin/developer/keys/(?P<id>\d+)', [
            'methods' => 'DELETE', 'callback' => [ __CLASS__, 'revoke_key' ], 'permission_callback' => [ 'Arag_Shop_Controller', 'admin_perm' ],
        ] );
        register_rest_route( $ns, '/arag/admin/developer/webhooks', [
            [ 'methods' => 'GET', 'callback' => [ __CLASS__, 'list_webhooks' ], 'permission_callback' => [ 'Arag_Shop_Controller', 'admin_perm' ] ],
            [ 'methods' => 'POST', 'callback' => [ __CLASS__, 'create_webhook' ], 'permission_callback' => [ 'Arag_Shop_Controller', 'admin_perm' ] ],
        ] );
        register_rest_route( $ns, '/arag/admin/developer/webhooks/(?P<id>\d+)', [
            'methods' => 'DELETE', 'callback' => [ __CLASS__, 'delete_webhook' ], 'permission_callback' => [ 'Arag_Shop_Controller', 'admin_perm' ],
        ] );
        register_rest_route( $ns, '/arag/admin/developer/webhooks/(?P<id>\d+)/test', [
            'methods' => 'POST', 'callback' => [ __CLASS__, 'test_webhook' ], 'permission_callback' => [ 'Arag_Shop_Controller', 'admin_perm' ],
        ] );
        register_rest_route( $ns, '/arag/admin/developer/webhook-log', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'webhook_log' ], 'permission_callback' => [ 'Arag_Shop_Controller', 'admin_perm' ],
        ] );
        register_rest_route( $ns, '/arag/admin/platform-info', [
            'methods' => 'GET', 'callback' => [ __CLASS__, 'platform_info' ], 'permission_callback' => [ 'Arag_Shop_Controller', 'admin_perm' ],
        ] );
    }

    public static function features( WP_REST_Request $req ): WP_REST_Response {
        return new WP_REST_Response( [
            'success' => true,
            'data'    => Acc_Shop_Platform::plan_comparison(),
            'current_plan' => Acc_Shop_Platform::current_plan(),
        ] );
    }

    public static function stats( WP_REST_Request $req ): WP_REST_Response {
        return new WP_REST_Response( [ 'success' => true, 'data' => Acc_Shop_Platform::showcase_stats() ] );
    }

    public static function docs( WP_REST_Request $req ): WP_REST_Response {
        return new WP_REST_Response( [ 'success' => true, 'data' => Acc_Shop_Platform::api_docs_index() ] );
    }

    public static function demo_request( WP_REST_Request $req ): WP_REST_Response {
        $body = $req->get_json_params() ?: [];
        $res  = Acc_Shop_Platform::create_lead( $body );
        return new WP_REST_Response( $res, ! empty( $res['success'] ) ? 201 : 400 );
    }

    public static function get_branding( WP_REST_Request $req ): WP_REST_Response {
        return new WP_REST_Response( [ 'success' => true, 'data' => Arag_Shop::get_settings() ] );
    }

    public static function save_branding( WP_REST_Request $req ): WP_REST_Response {
        $body = $req->get_json_params() ?: [];
        $saved = Arag_Shop::save_settings( $body );
        return new WP_REST_Response( [ 'success' => true, 'data' => $saved ] );
    }

    public static function platform_info( WP_REST_Request $req ): WP_REST_Response {
        return new WP_REST_Response( [
            'success'      => true,
            'plan'         => Acc_Shop_Platform::current_plan(),
            'plan_detail'  => Acc_Shop_Platform::plans()[ Acc_Shop_Platform::current_plan() ] ?? null,
            'features'     => Acc_Shop_Platform::feature_labels(),
            'has_api'      => Acc_Shop_Platform::has_feature( 'api_keys' ),
            'has_webhooks' => Acc_Shop_Platform::has_feature( 'webhooks' ),
            'webhook_events' => Acc_Shop_Platform::webhook_events(),
        ] );
    }

    public static function list_keys( WP_REST_Request $req ): WP_REST_Response {
        return new WP_REST_Response( [ 'success' => true, 'data' => Acc_Shop_Platform::list_api_keys() ] );
    }

    public static function create_key( WP_REST_Request $req ): WP_REST_Response {
        $body = $req->get_json_params() ?: [];
        $res  = Acc_Shop_Platform::create_api_key( $body['label'] ?? '', $body['scopes'] ?? [] );
        return new WP_REST_Response( $res, ! empty( $res['success'] ) ? 201 : 400 );
    }

    public static function revoke_key( WP_REST_Request $req ): WP_REST_Response {
        Acc_Shop_Platform::revoke_api_key( (int) $req->get_param( 'id' ) );
        return new WP_REST_Response( [ 'success' => true ] );
    }

    public static function list_webhooks( WP_REST_Request $req ): WP_REST_Response {
        return new WP_REST_Response( [
            'success' => true,
            'data'    => Acc_Shop_Platform::list_webhooks(),
            'events'  => Acc_Shop_Platform::webhook_events(),
        ] );
    }

    public static function create_webhook( WP_REST_Request $req ): WP_REST_Response {
        $body = $req->get_json_params() ?: [];
        $res  = Acc_Shop_Platform::create_webhook( $body );
        return new WP_REST_Response( $res, ! empty( $res['success'] ) ? 201 : 400 );
    }

    public static function delete_webhook( WP_REST_Request $req ): WP_REST_Response {
        Acc_Shop_Platform::delete_webhook( (int) $req->get_param( 'id' ) );
        return new WP_REST_Response( [ 'success' => true ] );
    }

    public static function test_webhook( WP_REST_Request $req ): WP_REST_Response {
        $res = Acc_Shop_Platform::test_webhook( (int) $req->get_param( 'id' ) );
        return new WP_REST_Response( $res, ! empty( $res['success'] ) ? 200 : 400 );
    }

    public static function webhook_log( WP_REST_Request $req ): WP_REST_Response {
        return new WP_REST_Response( [ 'success' => true, 'data' => Acc_Shop_Platform::webhook_log() ] );
    }
}
