<?php

namespace App\Http\Controllers;

use App\Models\Restaurante;
use App\Models\Compra;
use App\Models\Proveedor;
use App\Models\Factura;
use App\Services\PaginatorService;
use App\Services\UuidService;
use App\Exceptions\GenericException;
use App\Exceptions\CantdeletehasChild;
use App\Exceptions\AlreadyExistsException;
use App\Exceptions\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Ramsey\Uuid\Uuid;

class FacturasController extends Controller {

    /**
     * Obtiene la factura de una compra
     */
    public function get(Request $request, $restaurante_id, $id) {
        app(UuidService::class)->validOrFail($restaurante_id);
        app(UuidService::class)->validOrFail($id);

        $restaurante = Restaurante::findOrFail($restaurante_id);
        $compra = Compra::findOrFail($id);

        if($compra->restaurante != $restaurante) {
            throw new ModelNotFoundException("compra", $id);
        }

        if($compra->facturas()->count() == 0) {
            throw new ModelNotFoundException("factura", null);
        }

        return response()->json($compra->facturas()->first());
    }

    /**
     * Crea una factura a una compra
     */
    public function create(Request $request, $restaurante_id, $id) {
        $this->validate($request, [
            'numero' => 'required',
            'monto_bruto' => 'required|numeric',
        ]);

        app(UuidService::class)->validOrFail($restaurante_id);
        app(UuidService::class)->validOrFail($id);

        $restaurante = Restaurante::findOrFail($restaurante_id);
        $compra = Compra::findOrFail($id);

        if($compra->restaurante != $restaurante) {
            throw new ModelNotFoundException("compra", $id);
        }

        if($compra->facturas()->count() > 0) {
            throw new AlreadyExistsException("factura");
        }

        $factura = Factura::create([
            'id' => Uuid::uuid4(),
            'numero' => $request->input('numero'),
            'monto_bruto' => $request->input('monto_bruto'),
            'compra_id' => $compra->id
        ]);

        return response()->json($factura, 201);
    }

    /**
     * Modifica la factura a una compra
     */
    public function update(Request $request, $restaurante_id, $id) {
        $this->validate($request, [
            'numero' => 'required',
            'monto_bruto' => 'required|numeric',
        ]);

        app(UuidService::class)->validOrFail($restaurante_id);
        app(UuidService::class)->validOrFail($id);

        $restaurante = Restaurante::findOrFail($restaurante_id);
        $compra = Compra::findOrFail($id);

        if($compra->restaurante != $restaurante) {
            throw new ModelNotFoundException("compra", $id);
        }

        if($compra->facturas()->count() == 0) {
            throw new ModelNotFoundException("factura", null);
        }

        $factura = $compra->facturas()->first();
        $factura->numero = $request->input('numero');
        $factura->monto_bruto = $request->input('monto_bruto');
        $factura->save();

        return response()->json($factura, 201);
    }

    
    /**
     * Elimina la factura de una compra
     */
    public function delete(Request $request, $restaurante_id, $id) {
        app(UuidService::class)->validOrFail($restaurante_id);
        app(UuidService::class)->validOrFail($id);

        $restaurante = Restaurante::findOrFail($restaurante_id);
        $compra = Compra::findOrFail($id);

        if($compra->restaurante != $restaurante) {
            throw new ModelNotFoundException("compra", $id);
        }

        if($compra->facturas()->count() == 0) {
            throw new ModelNotFoundException("factura", null);
        }

        $factura = $compra->facturas()->first();
        $factura->delete();

        return response()->json([], 204);
    }
}
