<?php
declare(strict_types=1);

namespace App\Modules\Settings;

use PDO;

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

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

        $statement = $this->pdo->prepare(
            'SELECT shoppro_status_code, shoppro_status_name, orderpro_status_code
            FROM order_status_mappings
            WHERE integration_id = :integration_id
            ORDER BY shoppro_status_code ASC'
        );
        $statement->execute(['integration_id' => $integrationId]);
        $rows = $statement->fetchAll(PDO::FETCH_ASSOC);

        if (!is_array($rows)) {
            return [];
        }

        $result = [];
        foreach ($rows as $row) {
            if (!is_array($row)) {
                continue;
            }

            $shopproCode = trim((string) ($row['shoppro_status_code'] ?? ''));
            if ($shopproCode === '') {
                continue;
            }

            $result[] = [
                'shoppro_status_code' => $shopproCode,
                'shoppro_status_name' => trim((string) ($row['shoppro_status_name'] ?? '')),
                'orderpro_status_code' => trim((string) ($row['orderpro_status_code'] ?? '')),
            ];
        }

        return $result;
    }

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

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

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

        $insertStatement = $this->pdo->prepare(
            'INSERT INTO order_status_mappings (
                integration_id, shoppro_status_code, shoppro_status_name, orderpro_status_code, created_at, updated_at
            ) VALUES (
                :integration_id, :shoppro_status_code, :shoppro_status_name, :orderpro_status_code, NOW(), NOW()
            )'
        );

        foreach ($mappings as $mapping) {
            $shopproCode = trim((string) ($mapping['shoppro_status_code'] ?? ''));
            $orderproCode = trim((string) ($mapping['orderpro_status_code'] ?? ''));
            if ($shopproCode === '' || $orderproCode === '') {
                continue;
            }

            $shopproName = trim((string) ($mapping['shoppro_status_name'] ?? ''));
            $insertStatement->execute([
                'integration_id' => $integrationId,
                'shoppro_status_code' => $shopproCode,
                'shoppro_status_name' => $shopproName !== '' ? $shopproName : null,
                'orderpro_status_code' => $orderproCode,
            ]);
        }
    }
}
