<?php

namespace App\Http\Controllers\Care;

use App\Http\Controllers\Controller;
use App\Models\CareServiceQueue;
use App\Services\Care\CareQueueEngine;
use App\Services\Care\DeviceService;
use App\Services\Care\KioskService;
use App\Support\OrganizationBranding;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class KioskDeviceController extends Controller
{
    public function __construct(
        protected CareQueueEngine $engine,
        protected DeviceService $devices,
        protected KioskService $kiosk,
    ) {}

    public function show(Request $request): View
    {
        $device = $request->attributes->get('care.device');
        $device->loadMissing('organization');
        $queues = $this->kiosk->queuesFor($device);
        $settings = $this->kiosk->settings($device);
        $organization = $device->organization;
        $token = $this->devices->publicAccessToken($device) ?? (string) $request->route('token');

        $this->devices->recordHeartbeat($device);

        return view('care.kiosk.device', [
            'device' => $device,
            'organization' => $organization,
            'queues' => $queues,
            'settings' => $settings,
            'kioskToken' => $token,
            'logoUrl' => $organization
                ? OrganizationBranding::logoUrl($organization)
                : asset(OrganizationBranding::DEFAULT_LOGO),
            'logoAlt' => $organization
                ? OrganizationBranding::logoAlt($organization)
                : 'Ladill Care',
        ]);
    }

    public function issue(Request $request): JsonResponse
    {
        $device = $request->attributes->get('care.device');
        $this->devices->recordHeartbeat($device);

        $validated = $request->validate([
            'queue_id' => ['required', 'integer', 'exists:care_service_queues,id'],
            'customer_name' => ['nullable', 'string', 'max:255'],
            'customer_phone' => ['nullable', 'string', 'max:50'],
            'priority' => ['nullable', 'string', 'max:50'],
        ]);

        abort_unless($this->kiosk->queueAllowedForIssue($device, (int) $validated['queue_id']), 403);

        $queue = CareServiceQueue::query()->findOrFail($validated['queue_id']);
        abort_unless($queue->is_active, 422, 'This queue is not accepting tickets right now.');

        $ticket = $this->engine->issueTicket($device->organization, [
            'service_queue_id' => $queue->uuid,
            'customer_name' => $validated['customer_name'] ?? null,
            'customer_phone' => $validated['customer_phone'] ?? null,
            'priority' => $validated['priority'] ?? 'walk_in',
            'source' => 'kiosk',
            'metadata' => [
                'device_id' => $device->id,
                'device_name' => $device->name,
            ],
        ]);

        $ticket['queue'] = [
            'uuid' => $queue->uuid,
            'name' => $queue->name,
            'prefix' => $queue->prefix,
        ];

        return response()->json(['data' => $ticket]);
    }
}
