import { Router } from "express";
import { CommonCodeController } from "../controllers/commonCodeController";
import { authenticateToken } from "../middleware/authMiddleware";

const router = Router();
const commonCodeController = new CommonCodeController();

// 모든 공통코드 API는 인증이 필요
router.use(authenticateToken);

// 카테고리 관련 라우트
router.get("/categories", (req, res) =>
  commonCodeController.getCategories(req, res)
);

// 카테고리 중복 검사 (구체적인 경로를 먼저 배치)
router.get("/categories/check-duplicate", (req, res) =>
  commonCodeController.checkCategoryDuplicate(req, res)
);

router.post("/categories", (req, res) =>
  commonCodeController.createCategory(req, res)
);
router.put("/categories/:categoryCode", (req, res) =>
  commonCodeController.updateCategory(req, res)
);
router.delete("/categories/:categoryCode", (req, res) =>
  commonCodeController.deleteCategory(req, res)
);

// 코드 관련 라우트
router.get("/categories/:categoryCode/codes", (req, res) =>
  commonCodeController.getCodes(req, res)
);
router.post("/categories/:categoryCode/codes", (req, res) =>
  commonCodeController.createCode(req, res)
);

// 코드 중복 검사 (구체적인 경로를 먼저 배치)
router.get("/categories/:categoryCode/codes/check-duplicate", (req, res) =>
  commonCodeController.checkCodeDuplicate(req, res)
);

// 코드 순서 변경 (구체적인 경로를 먼저 배치)
router.put("/categories/:categoryCode/codes/reorder", (req, res) =>
  commonCodeController.reorderCodes(req, res)
);

// 계층구조 코드 조회 (구체적인 경로를 먼저 배치)
router.get("/categories/:categoryCode/hierarchy", (req, res) =>
  commonCodeController.getHierarchicalCodes(req, res)
);

// 코드 트리 조회
router.get("/categories/:categoryCode/tree", (req, res) =>
  commonCodeController.getCodeTree(req, res)
);

// 자식 코드 존재 여부 확인
router.get("/categories/:categoryCode/codes/:codeValue/has-children", (req, res) =>
  commonCodeController.hasChildren(req, res)
);

router.put("/categories/:categoryCode/codes/:codeValue", (req, res) =>
  commonCodeController.updateCode(req, res)
);
router.delete("/categories/:categoryCode/codes/:codeValue", (req, res) =>
  commonCodeController.deleteCode(req, res)
);

// 화면관리용 옵션 조회
router.get("/categories/:categoryCode/options", (req, res) =>
  commonCodeController.getCodeOptions(req, res)
);

export default router;
