<?php

namespace App\Services;

use App\Models\Coupon;
use Illuminate\Database\Eloquent\Collection;

class SmsBroadcastService
{
    /**
     * @param  array<int, int|string>  $selectedIds
     * @param  array<int, int|string>  $excludedIds
     * @return Collection<int, Coupon>
     */
    public function resolveRecipients(bool $sendToAll, array $selectedIds = [], array $excludedIds = []): Collection
    {
        $excludedIds = array_values(array_unique(array_map('intval', $excludedIds)));

        if ($sendToAll) {
            $query = Coupon::query();
        } else {
            $selectedIds = array_values(array_unique(array_map('intval', $selectedIds)));
            $query = Coupon::query()->whereIn('id', $selectedIds);
        }

        if ($excludedIds !== []) {
            $query->whereNotIn('id', $excludedIds);
        }

        return $query->orderBy('id')->get();
    }
}
