<?php
namespace Domain\ShopStatus;

class ShopStatusRepository
{
    private const MAX_PER_PAGE = 100;

    private $db;

    public function __construct($db)
    {
        $this->db = $db;
    }

    /**
     * @return array{items: array<int, array<string, mixed>>, total: int}
     */
    public function listForAdmin(
        array $filters,
        string $sortColumn = 'o',
        string $sortDir = 'ASC',
        int $page = 1,
        int $perPage = 15
    ): array {
        $allowedSortColumns = [
            'id' => 'ss.id',
            'status' => 'ss.status',
            'color' => 'ss.color',
            'o' => 'ss.o',
            'apilo_status_id' => 'ss.apilo_status_id',
        ];

        $sortSql = $allowedSortColumns[$sortColumn] ?? 'ss.o';
        $sortDir = strtoupper(trim($sortDir)) === 'DESC' ? 'DESC' : 'ASC';
        $page = max(1, $page);
        $perPage = min(self::MAX_PER_PAGE, max(1, $perPage));
        $offset = ($page - 1) * $perPage;

        $where = ['1 = 1'];
        $params = [];

        $status = trim((string)($filters['status'] ?? ''));
        if ($status !== '') {
            if (strlen($status) > 255) {
                $status = substr($status, 0, 255);
            }
            $where[] = 'ss.status LIKE :status';
            $params[':status'] = '%' . $status . '%';
        }

        $whereSql = implode(' AND ', $where);

        $sqlCount = "
            SELECT COUNT(0)
            FROM pp_shop_statuses AS ss
            WHERE {$whereSql}
        ";

        $stmtCount = $this->db->query($sqlCount, $params);
        $countRows = $stmtCount ? $stmtCount->fetchAll() : [];
        $total = isset($countRows[0][0]) ? (int)$countRows[0][0] : 0;

        $sql = "
            SELECT
                ss.id,
                ss.status,
                ss.color,
                ss.o,
                ss.apilo_status_id
            FROM pp_shop_statuses AS ss
            WHERE {$whereSql}
            ORDER BY {$sortSql} {$sortDir}, ss.id ASC
            LIMIT {$perPage} OFFSET {$offset}
        ";

        $stmt = $this->db->query($sql, $params);
        $items = $stmt ? $stmt->fetchAll() : [];

        if (!is_array($items)) {
            $items = [];
        }

        foreach ($items as &$item) {
            $item['id'] = (int)($item['id'] ?? 0);
            $item['apilo_status_id'] = $item['apilo_status_id'] !== null
                ? (int)$item['apilo_status_id']
                : null;
            $item['o'] = (int)($item['o'] ?? 0);
        }
        unset($item);

        return [
            'items' => $items,
            'total' => $total,
        ];
    }

    public function find(int $statusId): ?array
    {
        if ($statusId < 0) {
            return null;
        }

        $status = $this->db->get('pp_shop_statuses', '*', ['id' => $statusId]);
        if (!is_array($status)) {
            return null;
        }

        $status['id'] = (int)($status['id'] ?? 0);
        $status['apilo_status_id'] = $status['apilo_status_id'] !== null
            ? (int)$status['apilo_status_id']
            : null;
        $status['o'] = (int)($status['o'] ?? 0);

        return $status;
    }

    public function save(int $statusId, array $data): int
    {
        if ($statusId < 0) {
            return 0;
        }

        $row = [
            'color' => trim((string)($data['color'] ?? '')),
            'apilo_status_id' => isset($data['apilo_status_id']) && $data['apilo_status_id'] !== ''
                ? (int)$data['apilo_status_id']
                : null,
        ];

        $this->db->update('pp_shop_statuses', $row, ['id' => $statusId]);

        return $statusId;
    }

    /**
     * Pobiera Apilo status ID dla danego statusu sklepowego.
     * Odpowiednik front\factory\ShopStatuses::get_apilo_status_id()
     */
    public function getApiloStatusId(int $statusId): ?int
    {
        $value = $this->db->get('pp_shop_statuses', 'apilo_status_id', ['id' => $statusId]);
        return $value !== null && $value !== false ? (int)$value : null;
    }

    /**
     * Pobiera shop status ID na podstawie ID statusu integracji.
     * Odpowiednik front\factory\ShopStatuses::get_shop_status_by_integration_status_id()
     */
    public function getByIntegrationStatusId(string $integration, int $integrationStatusId): ?int
    {
        if ($integration === 'apilo') {
            $value = $this->db->get('pp_shop_statuses', 'id', [
                'apilo_status_id' => $integrationStatusId,
            ]);
            return $value !== null && $value !== false ? (int)$value : null;
        }

        return null;
    }

    /**
     * Zwraca liste wszystkich statusow (id => nazwa) posortowanych wg kolejnosci.
     * Odpowiednik shop\Order::order_statuses()
     */
    public function allStatuses(): array
    {
        $results = $this->db->select('pp_shop_statuses', ['id', 'status'], [
            'ORDER' => ['o' => 'ASC'],
        ]);

        $statuses = [];
        if (is_array($results)) {
            foreach ($results as $row) {
                $statuses[(int)$row['id']] = $row['status'];
            }
        }

        return $statuses;
    }
}
