<?php

namespace App\Services\Care;

use App\Models\CareServiceQueue;
use App\Models\Device;
use Illuminate\Support\Collection;

class KioskService
{
    /**
     * @return array{service_queue_ids: list<int>, collect_name: bool, collect_phone: bool, reset_seconds: int, welcome_message: ?string}
     */
    public function settings(Device $device): array
    {
        $config = $device->metadata ?? [];

        return [
            'service_queue_ids' => array_values(array_filter(array_map(
                'intval',
                $config['service_queue_ids'] ?? [],
            ))),
            'collect_name' => (bool) ($config['collect_name'] ?? false),
            'collect_phone' => (bool) ($config['collect_phone'] ?? false),
            'reset_seconds' => max(5, (int) ($config['reset_seconds'] ?? 15)),
            'welcome_message' => filled($config['welcome_message'] ?? null)
                ? (string) $config['welcome_message']
                : null,
        ];
    }

    /**
     * @return list<int>
     */
    public function assignedQueueIds(Device $device): array
    {
        return $this->settings($device)['service_queue_ids'];
    }

    /**
     * @return Collection<int, CareServiceQueue>
     */
    public function queuesFor(Device $device): Collection
    {
        $ids = $this->assignedQueueIds($device);

        return CareServiceQueue::query()
            ->where('organization_id', $device->organization_id)
            ->where('is_active', true)
            ->when($device->branch_id, fn ($q) => $q->where('branch_id', $device->branch_id))
            ->when($ids !== [], fn ($q) => $q->whereIn('id', $ids))
            ->orderBy('name')
            ->get();
    }

    public function queueAllowedForIssue(Device $device, int $queueId): bool
    {
        $ids = $this->assignedQueueIds($device);

        return CareServiceQueue::query()
            ->where('organization_id', $device->organization_id)
            ->where('id', $queueId)
            ->where('is_active', true)
            ->when($device->branch_id, fn ($q) => $q->where('branch_id', $device->branch_id))
            ->when($ids !== [], fn ($q) => $q->whereIn('id', $ids))
            ->exists();
    }

    /**
     * @param  array<string, mixed>  $input
     * @return array<string, mixed>
     */
    public function buildMetadata(array $input, array $allowedQueueIds = []): array
    {
        $queueIds = array_values(array_intersect(
            array_map('intval', $input['queue_ids'] ?? []),
            $allowedQueueIds,
        ));

        return [
            'service_queue_ids' => $queueIds,
            'collect_name' => (bool) ($input['collect_name'] ?? false),
            'collect_phone' => (bool) ($input['collect_phone'] ?? false),
            'reset_seconds' => max(5, min(120, (int) ($input['reset_seconds'] ?? 15))),
            'welcome_message' => filled($input['welcome_message'] ?? null)
                ? (string) $input['welcome_message']
                : null,
        ];
    }
}
