<?php

namespace App\Http\Controllers\Api\Tenant;

use App\Http\Controllers\Controller;
use App\Http\Resources\Tenant\PhotoboothStatusResource;
use App\Models\Event;
use App\Models\PhotoboothSetting;
use App\Services\Photobooth\PhotoboothProvisioner;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PhotoboothController extends Controller
{
    public function __construct(private readonly PhotoboothProvisioner $provisioner) {}

    public function show(Request $request, Event $event): PhotoboothStatusResource
    {
        $this->assertEventBelongsToTenant($request, $event);

        return $this->resource($event);
    }

    public function enable(Request $request, Event $event): JsonResponse
    {
        $this->assertEventBelongsToTenant($request, $event);

        $event->loadMissing('tenant');
        $mode = $this->resolveMode($request);
        $updated = $mode === 'sparkbooth'
            ? $this->provisioner->enableSparkbooth($event)
            : $this->provisioner->enable($event);

        return response()->json([
            'message' => __('Photobooth-Zugang aktiviert.'),
            'data' => $this->resource($updated),
        ]);
    }

    public function rotate(Request $request, Event $event): JsonResponse
    {
        $this->assertEventBelongsToTenant($request, $event);

        $event->loadMissing('tenant');
        $mode = $this->resolveMode($request);
        $updated = $mode === 'sparkbooth'
            ? $this->provisioner->rotateSparkbooth($event)
            : $this->provisioner->rotate($event);

        return response()->json([
            'message' => __('Zugangsdaten neu generiert.'),
            'data' => $this->resource($updated),
        ]);
    }

    public function disable(Request $request, Event $event): JsonResponse
    {
        $this->assertEventBelongsToTenant($request, $event);

        $event->loadMissing('tenant');
        $mode = $this->resolveMode($request);
        $updated = $mode === 'sparkbooth'
            ? $this->provisioner->disableSparkbooth($event)
            : $this->provisioner->disable($event);

        return response()->json([
            'message' => __('Photobooth-Zugang deaktiviert.'),
            'data' => $this->resource($updated),
        ]);
    }

    protected function resource(Event $event): PhotoboothStatusResource
    {
        return PhotoboothStatusResource::make([
            'event' => $event->fresh(),
            'settings' => PhotoboothSetting::current(),
        ]);
    }

    protected function assertEventBelongsToTenant(Request $request, Event $event): void
    {
        $tenantId = (int) $request->attributes->get('tenant_id');

        if ($tenantId !== (int) $event->tenant_id) {
            abort(403, 'Event gehört nicht zu diesem Tenant.');
        }
    }

    protected function resolveMode(Request $request): string
    {
        $mode = strtolower((string) $request->input('mode', $request->input('type', 'ftp')));

        return in_array($mode, ['sparkbooth', 'ftp'], true) ? $mode : 'ftp';
    }
}
