<?php

namespace App\Http\Controllers;

use App\Models\PromoQr;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class PromoQrController extends Controller
{
    public function index()
    {
        $promoQrs = PromoQr::with('promocion')->get();
        return response()->json($promoQrs);
    }

    public function show($id)
    {
        $promoQr = PromoQr::with('promocion', 'usuario')->findOrFail($id);
        return response()->json($promoQr);
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'id_promo' => 'required|integer|exists:promociones,id',
            'id_usuario' => 'required|integer',
            'tipo_usuario' => 'required|in:jugador,aficionado',
        ]);
        $data['id_qr'] = Str::uuid()->toString();
        $promoQr = PromoQr::create($data);
        return response()->json($promoQr, 201);
    }

    public function update(Request $request, $id)
    {
        $promoQr = PromoQr::findOrFail($id);
        $data = $request->validate([
            'usado' => 'nullable|boolean',
            'usado_en' => 'nullable',
        ]);
        $promoQr->update($data);
        return response()->json($promoQr);
    }

    public function destroy($id)
    {
        $promoQr = PromoQr::findOrFail($id);
        $promoQr->delete();
        return response()->json(null, 204);
    }
}
