<?php

namespace App\Http\Controllers\Supplier;

use Illuminate\Http\Request;
use App\Models\Supplier\Supplier;
use App\Models\Supplier\SupplierCategory;
use App\Http\Controllers\Controller;

class SupplierController extends Controller
{
    public function index(Request $request)
    {
        $search = $request->get("search");
        $idCategoriaProveedor = $request->get("idCategoriaProveedor");
        $state = $request->get("state");

        $suppliers = Supplier::filterMultiple($search, $idCategoriaProveedor, $state)
            ->with("categoria")
            ->orderBy("idProveedor", "desc")
            ->paginate(25);

        return response()->json([
            "total" => $suppliers->total(),
            "paginate" => 25,
            "suppliers" => $suppliers->map(function($supplier) {
                return [
                    "id" => $supplier->idProveedor,
                    "ruc" => $supplier->ruc,
                    "razonSocial" => $supplier->razonSocial,
                    "direccion" => $supplier->direccion,
                    "telefono" => $supplier->telefono,
                    "email" => $supplier->email,
                    "paginaWeb" => $supplier->paginaWeb,
                    "tipoDocumento" => $supplier->tipoDocumento,
                    "documentoRepresentante" => $supplier->documentoRepresentante,
                    "nombreRepresentante" => $supplier->nombreRepresentante,
                    "idCategoriaProveedor" => $supplier->idCategoriaProveedor,
                    "categoria" => $supplier->categoria ? [
                        "id" => $supplier->categoria->idCategoriaProveedor,
                        "nombre" => $supplier->categoria->nombre,
                    ] : null,
                    "state" => $supplier->state,
                    "created_at" => $supplier->created_at->format("Y/m/d h:i A")
                ];
            })
        ]);
    }

    public function config()
    {
        $categories = SupplierCategory::where("state", 1)->get();
        return response()->json([
            "categories" => $categories->map(function($categorie) {
                return [
                    "id" => $categorie->idCategoriaProveedor,
                    "nombre" => $categorie->nombre,
                ];
            })
        ]);
    }

    public function store(Request $request)
    {
        $is_exists = Supplier::where("ruc", $request->ruc)->first();
        if ($is_exists) {
            return response()->json([
                "code" => 405,
                "message" => "EL RUC DEL PROVEEDOR YA EXISTE"
            ]);
        }

        $supplier = Supplier::create($request->all());

        return response()->json([
            "code" => 200,
            "message" => "El proveedor se ha creado correctamente"
        ]);
    }

    public function show(string $id)
    {
        $supplier = Supplier::with("categoria")->find($id);
        if (!$supplier) {
            return response()->json(["code" => 405, "message" => "El proveedor no existe"]);
        }

        return response()->json([
            "supplier" => [
                "id" => $supplier->idProveedor,
                "ruc" => $supplier->ruc,
                "razonSocial" => $supplier->razonSocial,
                "direccion" => $supplier->direccion,
                "telefono" => $supplier->telefono,
                "email" => $supplier->email,
                "paginaWeb" => $supplier->paginaWeb,
                "tipoDocumento" => $supplier->tipoDocumento,
                "documentoRepresentante" => $supplier->documentoRepresentante,
                "nombreRepresentante" => $supplier->nombreRepresentante,
                "idCategoriaProveedor" => $supplier->idCategoriaProveedor,
                "state" => $supplier->state,
            ],
            "code" => 200
        ]);
    }

    public function update(Request $request, string $id)
    {
        $is_exists = Supplier::where("idProveedor", "<>", $id)->where("ruc", $request->ruc)->first();
        if ($is_exists) {
            return response()->json([
                "code" => 405,
                "message" => "EL RUC DEL PROVEEDOR YA EXISTE"
            ]);
        }

        $supplier = Supplier::findOrFail($id);
        $supplier->update($request->all());

        return response()->json([
            "code" => 200,
            "message" => "El proveedor se ha actualizado correctamente"
        ]);
    }

    public function destroy(string $id)
    {
        $supplier = Supplier::findOrFail($id);
        $supplier->delete();
        return response()->json([
            "code" => 200,
            "message" => "El proveedor se ha eliminado correctamente"
        ]);
    }
}
