<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Services\PrestashopService;
use App\Services\GravureService;
use App\Services\MailService;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class PrestashopController
{
    use ControllerTrait;

    public function __construct(
        private PrestashopService $ps,
        private GravureService    $gravure,
        private MailService       $mail
    ) {}


    // GET /api/cron/sync-commandes?secret=XXX
    public function cronSync(Request $request, Response $response): Response
    {
        $secret = $request->getQueryParams()['secret'] ?? '';
        $expected = $_ENV['CRON_SECRET'] ?? '';

        if (!$expected || !hash_equals($expected, $secret)) {
            return $this->error($response, 'FORBIDDEN', 'Secret invalide', 403);
        }

        try {
            $result = $this->ps->syncCommandes($this->gravure, $this->mail);
            return $this->success($response, $result);
        } catch (\Exception $e) {
            return $this->error($response, 'PS_CONNECTION_FAILED', $e->getMessage(), 502);
        }
    }

    public function syncCommandes(Request $request, Response $response): Response
    {
        $guard = $this->requireRole($request, $response, ['admin_fab', 'super_admin']);
        if ($guard) return $guard;

        try {
            $result = $this->ps->syncCommandes($this->gravure, $this->mail);
            return $this->success($response, $result);
        } catch (\Exception $e) {
            return $this->error($response, 'PS_CONNECTION_FAILED', $e->getMessage(), 502);
        }
    }

    public function syncCommandesB2b(Request $request, Response $response): Response
    {
        $guard = $this->requireRole($request, $response, ['admin_fab', 'super_admin']);
        if ($guard) return $guard;

        try {
            $result = $this->ps->syncCommandes($this->gravure, $this->mail);
            return $this->success($response, ['importees' => $result['importees'], 'ignorees' => $result['ignorees']]);
        } catch (\Exception $e) {
            return $this->error($response, 'PS_CONNECTION_FAILED', $e->getMessage(), 502);
        }
    }

    public function testConnexion(Request $request, Response $response): Response
    {
        $guard = $this->requireRole($request, $response, ['admin_fab', 'super_admin']);
        if ($guard) return $guard;

        $result = $this->ps->testerConnexion();

        if (!$result['connected']) {
            return $this->error($response, 'PS_CONNECTION_FAILED', $result['error'] ?? 'Connexion impossible', 502);
        }

        return $this->success($response, $result);
    }

    public function testProduit(Request $request, Response $response): Response
    {
        $guard = $this->requireRole($request, $response, ['admin_fab', 'super_admin']);
        if ($guard) return $guard;

        $body      = (array)$request->getParsedBody();
        $productId = (int)($body['product_id'] ?? 0);

        if (!$productId) {
            return $this->error($response, 'MISSING_PRODUCT_ID', 'product_id requis', 422);
        }

        $result = $this->ps->testerProduit($productId);

        if (!$result['found']) {
            return $this->error($response, 'PRODUCT_NOT_FOUND', 'Produit PS ID ' . $productId . ' introuvable', 404);
        }

        return $this->success($response, $result);
    }
}
