import SearchUser from "../../Components/Global/SearchUser/searchUser";
import Receipt from "../../Components/Receipt/Receipt";
import Toggle from "../../Components/Global/Toggle/Toggle";
import Information from "../../Components/Global/Information/information";
import Impressions from "@/app/Components/Impressions/impressions";
import ShowError from "@/app/Components/Global/ShowError";

import { cookies } from "next/headers";
import { getCost } from "@/app/lib/getCost";
import { GetStudentWhitoutSancion } from "@/app/lib/getStudentWhitoutSancion";

import "@/app/globals.css";

export default async function Page(props: {
  searchParams?: Promise<{
    key: string;
    numAcount: string;
  }>;
}) {
  const params = await props.searchParams;
  const key = params?.key && params.key;
  const numAcount = params?.numAcount ? params.numAcount : null;

  const cookieStore = await cookies();
  const role = cookieStore.get("role")?.value || "";
  const roleNumber = parseInt(role);

  let student: any = null;
  let errorMessage = "";
  let costos: any[] = [];

  if (numAcount) {
    const result = await GetStudentWhitoutSancion(parseInt(numAcount));
    if (result.error) {
      errorMessage = `${result.error}`;
    } else {
      student = result as Student;
    }
  }

  let id_servicio: number;

  switch (key) {
    case "B/N":
      id_servicio = 1;
      break;

    case "color":
      id_servicio = 2;
      break;

    case "Plotter":
      id_servicio = 3;
      break;

    case "Escaner":
      id_servicio = 6;
      break;
  }

  try {

    const response = await getCost();
    costos = response;

  } catch (error) {
    console.error("Error obteniendo costos:", error);
  }

  const getCostsByService = (id_servicio: number) => {
    return costos
      .filter((c) => c.id_servicio === id_servicio)
      .map((c) => ({
        value: Number(c.costo),
        id_costo: c.id_costo,
        fecha_registro: c.fecha_registro,
      }));
  };

  let options: any[] = [];

  if (student) {
    options = [
      {
        key: "Recibo",
        label: "Recibo",
        content: <Receipt numAcount={student.id_cuenta} />,
      },
      {
        key: "B/N",
        label: "Impresiones B/N",
        content: (
          <Impressions
            costs={getCostsByService(1)}
            numAcount={student.id_cuenta}
            id_servicio={1}
          />
        ),
      },
      {
        key: "color",
        label: "Impresiones color",
        content: (
          <Impressions
            key={2}
            costs={getCostsByService(2)}
            numAcount={student.id_cuenta}
            id_servicio={2}
          />
        ),
      },
      {
        key: "Plotter",
        label: "Plotter",
        content: (
          <Impressions
            key={3}
            costs={getCostsByService(3)}
            numAcount={student.id_cuenta}
            id_servicio={3}
          />
        ),
      },
      {
        key: "Escaner",
        label: "Escaner",
        content: (
          <Impressions
            costs={getCostsByService(6)}
            numAcount={student.id_cuenta}
            id_servicio={6}
          />
        ),
      },
    ];
  }

  const optionsToUse =
    roleNumber === 5
      ? options.filter(
        (option) => option.key === "Recibo" || option.key === "B/N"
      )
      : options;

  return (
    <section className="containerSection">
      {errorMessage && <ShowError key={Date.now()} message={errorMessage} />}

      <h2 className="title">IMPRESIONES Y PLOTEO</h2>
      <SearchUser value={numAcount} />

      {student && (
        <>
          <Information
            NoCuenta={student.id_cuenta}
            Nombre={student.nombre}
            Carrera={student.carrera.carrera}
            Credito={student.credito}
          />
          <div style={{ height: "10px" }}></div>

          <Toggle defaultView={key} options={optionsToUse} />
        </>
      )}
    </section>
  );
}
//IO
