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 {
  CreateCustomerRequestSchema,
  CustomerListRequestSchema,
  GetCustomerByIdRequestSchema,
  UpdateCustomerByIdParamsRequestSchema,
  UpdateCustomerByIdRequestSchema,
} from "../../../common/dto";
import { buildCustomerDependencies } from "../dependencies";
import {
  CreateCustomerController,
  GetCustomerController,
  ListCustomersController,
  UpdateCustomerController,
} from "./controllers";

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

  const deps = buildCustomerDependencies(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(/*authenticateJWT(),*/ enforceTenant() /*checkTabContext*/);
  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(CustomerListRequestSchema, "params"),
    (req: Request, res: Response, next: NextFunction) => {
      const useCase = deps.build.list();
      const controller = new ListCustomersController(useCase /*,  deps.presenters.list */);
      return controller.execute(req, res, next);
    }
  );

  router.get(
    "/:customer_id",
    //checkTabContext,

    validateRequest(GetCustomerByIdRequestSchema, "params"),
    (req: Request, res: Response, next: NextFunction) => {
      const useCase = deps.build.get();
      const controller = new GetCustomerController(useCase);
      return controller.execute(req, res, next);
    }
  );

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

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

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

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

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

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

  app.use(`${baseRoutePath}/customers`, router);
};
