<?php

namespace App\Services\Admin\Sms;

use App\Repositories\Admin\Sms\AdminSmsBatchRepository;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;

final class AdminSmsLogService
{
    public function __construct(
        private readonly AdminSmsBatchRepository $repo,
    ) {}

    public function getStatusLabels(): array
    {
        return [
            'scheduled'  => '예약대기',
            'queued'     => '대기',
            'submitting' => '전송중',
            'submitted'  => '전송완료',
            'partial'    => '일부실패',
            'failed'     => '실패',
            'canceled'   => '취소',
        ];
    }

    public function getItemStatusLabels(): array
    {
        return [
            'queued'     => '대기',
            'submitted'  => '성공',
            'failed'     => '실패',
            'canceled'   => '취소',
            'skipped'    => '스킵',
        ];
    }

    public function getModeLabels(): array
    {
        return [
            'one'      => '단건',
            'many'     => '대량',
            'template' => '템플릿',
        ];
    }

    public function paginateBatches(array $filters, int $perPage = 30): LengthAwarePaginator
    {
        $page = $this->repo->paginateBatches($filters, $perPage);

        // ✅ 페이지 이동 시 필터 유지
        $clean = array_filter($filters, fn($v) => $v !== null && $v !== '');
        $page->appends($clean);

        return $page;
    }

    public function getBatch(int $batchId): ?object
    {
        return $this->repo->findBatch($batchId);
    }

    public function paginateItems(int $batchId, array $filters, int $perPage = 50): LengthAwarePaginator
    {
        $page = $this->repo->paginateItems($batchId, $filters, $perPage);

        // ✅ 상세 페이지에서도 필터 유지
        $clean = array_filter($filters, fn($v) => $v !== null && $v !== '');
        $page->appends($clean);

        return $page;
    }
}
