<?php

namespace App\Http\Controllers\Partner;

use App\Http\Controllers\Controller;
use App\Models\ServiceRequest;
use Illuminate\Http\Request;

class RequestController extends Controller
{
    public function index(Request $request, string $type)
    {
        $this->validateType($type);

        $requests = ServiceRequest::where('partner_id', $request->user()->partner_id)
            ->where('type', $type)
            ->latest()
            ->paginate(15);

        return view('partner.requests.index', [
            'requests' => $requests,
            'type' => $type,
            'typeLabel' => ServiceRequest::types()[$type],
        ]);
    }

    public function create(string $type)
    {
        $this->validateType($type);

        return view('partner.requests.create', [
            'type' => $type,
            'typeLabel' => ServiceRequest::types()[$type],
        ]);
    }

    public function store(Request $request, string $type)
    {
        $this->validateType($type);

        $rules = ['urgency' => ['required', 'in:normal,urgent']];
        $details = [];

        switch ($type) {
            case ServiceRequest::TYPE_PRELEVEMENT:
                $rules += [
                    'estimated_quantity' => ['required', 'integer', 'min:1'],
                    'requested_slot' => ['required', 'string', 'max:100'],
                    'notes' => ['nullable', 'string', 'max:1000'],
                ];
                break;
            case ServiceRequest::TYPE_CONSOMMABLE:
                $rules += [
                    'consumable_name' => ['required', 'string', 'max:255'],
                    'estimated_quantity' => ['required', 'integer', 'min:1'],
                    'notes' => ['nullable', 'string', 'max:1000'],
                ];
                break;
            case ServiceRequest::TYPE_RECLAMATION:
                $rules += [
                    'category' => ['required', 'in:retard,perte,casse,erreur_etiquetage,autre'],
                    'description' => ['required', 'string', 'max:2000'],
                ];
                break;
            case ServiceRequest::TYPE_RESULTAT:
                $rules += [
                    'patient_reference' => ['required', 'string', 'max:255'],
                    'notes' => ['nullable', 'string', 'max:1000'],
                ];
                break;
        }

        $data = $request->validate($rules);
        $requestedQuantity = $data['estimated_quantity'] ?? null;
        unset($data['urgency'], $data['estimated_quantity']);
        $details = $data;

        $serviceRequest = ServiceRequest::create([
            'partner_id' => $request->user()->partner_id,
            'type' => $type,
            'status' => ServiceRequest::STATUS_EN_ATTENTE,
            'details' => $details,
            'requested_quantity' => $requestedQuantity,
            'urgency' => $request->input('urgency'),
            'requested_slot' => $data['requested_slot'] ?? null,
        ]);

        $serviceRequest->changeStatus(ServiceRequest::STATUS_EN_ATTENTE, $request->user(), 'Demande creee par le partenaire');

        return redirect()->route('partner.requests.index', $type)
            ->with('success', 'Votre demande a ete envoyee avec succes.');
    }

    private function validateType(string $type): void
    {
        abort_unless(array_key_exists($type, ServiceRequest::types()), 404);
    }
}
