<?php

namespace Modules\Integracoes\WooCommerce\Controllers;

use Gestoreasy\Core\Http\Request;
use Gestoreasy\Core\Http\Response;
use Gestoreasy\Core\Tenant\TenantContext;
use Modules\Integracoes\WooCommerce\Services\WooCommerceAPIService;

class WooCommerceController {
    public function getConfig(Request $req): Response {
        try {
            $tenantId = TenantContext::getTenantId();
            $service = new WooCommerceAPIService($tenantId);
            $config = $service->getConfig();

            return Response::json(['data' => $config]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    public function updateConfig(Request $req): Response {
        try {
            $tenantId = TenantContext::getTenantId();
            $body = is_array($req->body) ? $req->body : [];
            
            $service = new WooCommerceAPIService($tenantId);
            $service->updateConfig($body);

            return Response::json(['data' => ['success' => true]]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    public function listProdutos(Request $req): Response {
        try {
            $tenantId = TenantContext::getTenantId();
            $service = new WooCommerceAPIService($tenantId);
            $produtos = $service->listProdutos();

            return Response::json(['data' => $produtos]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    public function sincronizarProdutos(Request $req): Response {
        try {
            $tenantId = TenantContext::getTenantId();
            $body = is_array($req->body) ? $req->body : [];
            $productId = isset($body['product_id']) ? (int)$body['product_id'] : null;

            $service = new WooCommerceAPIService($tenantId);
            $result = $service->sincronizarProdutos($productId);

            return Response::json(['data' => $result]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    public function publicarProduto(Request $req): Response {
        try {
            $tenantId = TenantContext::getTenantId();
            $productId = (int)($req->get('params')[0] ?? 0);

            if (!$productId) {
                return new Response(422, ['error' => ['code' => 'validation_error', 'message' => 'product_id é obrigatório']]);
            }

            $service = new WooCommerceAPIService($tenantId);
            $result = $service->publicarProduto($productId);

            return Response::json(['data' => $result]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    public function listPedidos(Request $req): Response {
        try {
            $tenantId = TenantContext::getTenantId();
            $service = new WooCommerceAPIService($tenantId);
            $pedidos = $service->listPedidos();

            return Response::json(['data' => $pedidos]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    public function sincronizarPedidos(Request $req): Response {
        try {
            $tenantId = TenantContext::getTenantId();
            $service = new WooCommerceAPIService($tenantId);
            $result = $service->sincronizarPedidos();

            return Response::json(['data' => $result]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    public function importarPedido(Request $req): Response {
        try {
            $tenantId = TenantContext::getTenantId();
            $wcOrderId = (int)($req->get('params')[0] ?? 0);

            if (!$wcOrderId) {
                return new Response(422, ['error' => ['code' => 'validation_error', 'message' => 'order_id é obrigatório']]);
            }

            $service = new WooCommerceAPIService($tenantId);
            $result = $service->importarPedido($wcOrderId);

            return Response::json(['data' => $result]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    public function sincronizarEstoque(Request $req): Response {
        try {
            $tenantId = TenantContext::getTenantId();
            $body = is_array($req->body) ? $req->body : [];
            $productId = (int)($body['product_id'] ?? 0);

            if (!$productId) {
                return new Response(422, ['error' => ['code' => 'validation_error', 'message' => 'product_id é obrigatório']]);
            }

            $service = new WooCommerceAPIService($tenantId);
            $service->sincronizarEstoque($productId);

            return Response::json(['data' => ['success' => true]]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }

    public function webhook(Request $req): Response {
        try {
            $tenantId = TenantContext::getTenantId();
            $body = is_array($req->body) ? $req->body : [];

            $service = new WooCommerceAPIService($tenantId);
            $service->processarWebhook($body);

            return Response::json(['data' => ['success' => true]]);
        } catch (\Exception $e) {
            return new Response(500, ['error' => ['code' => 'error', 'message' => $e->getMessage()]]);
        }
    }
}

