import { RequestWithAuth, enforceTenant, enforceUser, mockUser } from "@erp/auth/api";
import { ModuleParams, validateRequest } from "@erp/core/api";
import { ILogger } from "@repo/rdx-logger";
import { Application, NextFunction, Request, Response, Router } from "express";
import { Sequelize } from "sequelize";
import {
  CreateCustomerInvoiceRequestSchema,
  CustomerInvoiceListRequestSchema,
  GetCustomerInvoiceByIdRequestSchema,
  ReportCustomerInvoiceByIdRequestSchema,
  UpdateCustomerInvoiceByIdParamsRequestSchema,
  UpdateCustomerInvoiceByIdRequestSchema,
} from "../../../common/dto";
import { buildCustomerInvoiceDependencies } from "../dependencies";
import {
  CreateCustomerInvoiceController,
  GetCustomerInvoiceController,
  ListCustomerInvoicesController,
  ReportCustomerInvoiceController,
  UpdateCustomerInvoiceController,
} from "./controllers";

export const customerInvoicesRouter = (params: ModuleParams) => {
  const { app, baseRoutePath, logger } = params as {
    app: Application;
    database: Sequelize;
    baseRoutePath: string;
    logger: ILogger;
  };

  const deps = buildCustomerInvoiceDependencies(params);

  const router: Router = Router({ mergeParams: true });

  // 🔐 Autenticación + Tenancy para TODO el router
  if (process.env.NODE_ENV === "development") {
    router.use(
      (req: Request, res: Response, next: NextFunction) =>
        mockUser(req as RequestWithAuth, res, next) // Debe ir antes de las rutas protegidas
    );
  }

  router.use([
    (req: Request, res: Response, next: NextFunction) =>
      enforceUser()(req as RequestWithAuth, res, next), // Debe ir antes de las rutas protegidas

    (req: Request, res: Response, next: NextFunction) =>
      enforceTenant()(req as RequestWithAuth, res, next), // Debe ir antes de las rutas protegidas
  ]);

  // ----------------------------------------------

  router.get(
    "/",
    //checkTabContext,
    validateRequest(CustomerInvoiceListRequestSchema, "params"),
    async (req: Request, res: Response, next: NextFunction) => {
      const useCase = deps.build.list();
      const controller = new ListCustomerInvoicesController(useCase /*,  deps.presenters.list */);
      return controller.execute(req, res, next);
    }
  );

  router.get(
    "/:invoice_id",
    //checkTabContext,
    validateRequest(GetCustomerInvoiceByIdRequestSchema, "params"),
    (req: Request, res: Response, next: NextFunction) => {
      const useCase = deps.build.get();
      const controller = new GetCustomerInvoiceController(useCase);
      return controller.execute(req, res, next);
    }
  );

  router.post(
    "/",
    //checkTabContext,

    validateRequest(CreateCustomerInvoiceRequestSchema, "body"),
    (req: Request, res: Response, next: NextFunction) => {
      const useCase = deps.build.create();
      const controller = new CreateCustomerInvoiceController(useCase);
      return controller.execute(req, res, next);
    }
  );

  router.put(
    "/:invoice_id",
    //checkTabContext,

    validateRequest(UpdateCustomerInvoiceByIdParamsRequestSchema, "params"),
    validateRequest(UpdateCustomerInvoiceByIdRequestSchema, "body"),
    (req: Request, res: Response, next: NextFunction) => {
      const useCase = deps.build.update();
      const controller = new UpdateCustomerInvoiceController(useCase);
      return controller.execute(req, res, next);
    }
  );

  /*router.delete(
    "/:invoice_id",
    //checkTabContext,

    validateRequest(DeleteCustomerInvoiceByIdRequestSchema, "params"),
    (req: Request, res: Response, next: NextFunction) => {
      const useCase = deps.build.delete();
      const controller = new DeleteCustomerInvoiceController(useCase);
      return controller.execute(req, res, next);
    }
  );*/

  router.get(
    "/:invoice_id/report",
    //checkTabContext,
    validateRequest(ReportCustomerInvoiceByIdRequestSchema, "params"),
    (req: Request, res: Response, next: NextFunction) => {
      const useCase = deps.build.report();
      const controller = new ReportCustomerInvoiceController(useCase);
      return controller.execute(req, res, next);
    }
  ); /*router.put(
    "/:invoice_id/issue",
    //checkTabContext,
    validateRequest(XXX, "params"),
    validateRequest(XXX, "body"),
    (req: Request, res: Response, next: NextFunction) => {
      const useCase = deps.build.issue();
      const controller = new IssueCustomerInvoiceController(useCase);
      return controller.execute(req, res, next);
    }
);
*/

  app.use(`${baseRoutePath}/customer-invoices`, router);
};
