<?php
declare(strict_types=1);

namespace App\Modules\Settings;

use PDO;

final class ShopproDeliveryMethodMappingRepository
{
    public function __construct(private readonly PDO $pdo)
    {
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    public function listMappings(int $integrationId): array
    {
        if ($integrationId <= 0) {
            return [];
        }

        $stmt = $this->pdo->prepare(
            'SELECT *
            FROM shoppro_delivery_method_mappings
            WHERE integration_id = :integration_id
            ORDER BY order_delivery_method ASC'
        );
        $stmt->execute(['integration_id' => $integrationId]);
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return is_array($rows) ? $rows : [];
    }

    /**
     * @param array<int, array<string, string>> $mappings
     */
    public function saveMappings(int $integrationId, array $mappings): void
    {
        if ($integrationId <= 0) {
            return;
        }

        $deleteStmt = $this->pdo->prepare(
            'DELETE FROM shoppro_delivery_method_mappings WHERE integration_id = :integration_id'
        );
        $deleteStmt->execute(['integration_id' => $integrationId]);

        if ($mappings === []) {
            return;
        }

        $insertStmt = $this->pdo->prepare(
            'INSERT INTO shoppro_delivery_method_mappings (
                integration_id, order_delivery_method, carrier, allegro_delivery_method_id,
                allegro_credentials_id, allegro_carrier_id, allegro_service_name
            ) VALUES (
                :integration_id, :order_delivery_method, :carrier, :allegro_delivery_method_id,
                :allegro_credentials_id, :allegro_carrier_id, :allegro_service_name
            )'
        );

        foreach ($mappings as $mapping) {
            $orderMethod = trim((string) ($mapping['order_delivery_method'] ?? ''));
            $allegroMethodId = trim((string) ($mapping['allegro_delivery_method_id'] ?? ''));
            if ($orderMethod === '' || $allegroMethodId === '') {
                continue;
            }

            $carrier = trim((string) ($mapping['carrier'] ?? 'allegro'));
            $insertStmt->execute([
                'integration_id' => $integrationId,
                'order_delivery_method' => $orderMethod,
                'carrier' => $carrier !== '' ? $carrier : 'allegro',
                'allegro_delivery_method_id' => $allegroMethodId,
                'allegro_credentials_id' => trim((string) ($mapping['allegro_credentials_id'] ?? '')),
                'allegro_carrier_id' => trim((string) ($mapping['allegro_carrier_id'] ?? '')),
                'allegro_service_name' => trim((string) ($mapping['allegro_service_name'] ?? '')),
            ]);
        }
    }

    /**
     * @return array<int, string>
     */
    public function getDistinctOrderDeliveryMethods(int $integrationId): array
    {
        if ($integrationId <= 0) {
            return [];
        }

        $stmt = $this->pdo->prepare(
            "SELECT DISTINCT external_carrier_id
             FROM orders
             WHERE external_carrier_id IS NOT NULL
               AND external_carrier_id <> ''
               AND source = 'shoppro'
               AND integration_id = :integration_id
             ORDER BY external_carrier_id ASC"
        );
        $stmt->execute(['integration_id' => $integrationId]);
        $rows = $stmt->fetchAll(PDO::FETCH_COLUMN);
        return is_array($rows) ? $rows : [];
    }
}
