import { Router } from "express";
import { authenticateToken } from "../middleware/authMiddleware";
import {
  // 화면 그룹
  getScreenGroups,
  getScreenGroup,
  createScreenGroup,
  updateScreenGroup,
  deleteScreenGroup,
  // 화면-그룹 연결
  addScreenToGroup,
  removeScreenFromGroup,
  updateScreenInGroup,
  // 필드 조인
  getFieldJoins,
  createFieldJoin,
  updateFieldJoin,
  deleteFieldJoin,
  // 데이터 흐름
  getDataFlows,
  createDataFlow,
  updateDataFlow,
  deleteDataFlow,
  // 화면-테이블 관계
  getTableRelations,
  createTableRelation,
  updateTableRelation,
  deleteTableRelation,
  // 화면 레이아웃 요약
  getScreenLayoutSummary,
  getMultipleScreenLayoutSummary,
  // 화면 서브 테이블 관계
  getScreenSubTables,
  // 메뉴-화면그룹 동기화
  syncScreenGroupsToMenuController,
  syncMenuToScreenGroupsController,
  getSyncStatusController,
  syncAllCompaniesController,
  // POP 전용 화면 그룹
  getPopScreenGroups,
  createPopScreenGroup,
  updatePopScreenGroup,
  deletePopScreenGroup,
  ensurePopRootGroup,
} from "../controllers/screenGroupController";

const router = Router();

// 모든 라우트에 인증 미들웨어 적용
router.use(authenticateToken);

// ============================================================
// 화면 그룹 (screen_groups)
// ============================================================
router.get("/groups", getScreenGroups);
router.get("/groups/:id", getScreenGroup);
router.post("/groups", createScreenGroup);
router.put("/groups/:id", updateScreenGroup);
router.delete("/groups/:id", deleteScreenGroup);

// ============================================================
// 화면-그룹 연결 (screen_group_screens)
// ============================================================
router.post("/group-screens", addScreenToGroup);
router.put("/group-screens/:id", updateScreenInGroup);
router.delete("/group-screens/:id", removeScreenFromGroup);

// ============================================================
// 필드 조인 설정 (screen_field_joins)
// ============================================================
router.get("/field-joins", getFieldJoins);
router.post("/field-joins", createFieldJoin);
router.put("/field-joins/:id", updateFieldJoin);
router.delete("/field-joins/:id", deleteFieldJoin);

// ============================================================
// 데이터 흐름 (screen_data_flows)
// ============================================================
router.get("/data-flows", getDataFlows);
router.post("/data-flows", createDataFlow);
router.put("/data-flows/:id", updateDataFlow);
router.delete("/data-flows/:id", deleteDataFlow);

// ============================================================
// 화면-테이블 관계 (screen_table_relations)
// ============================================================
router.get("/table-relations", getTableRelations);
router.post("/table-relations", createTableRelation);
router.put("/table-relations/:id", updateTableRelation);
router.delete("/table-relations/:id", deleteTableRelation);

// ============================================================
// 화면 레이아웃 요약 (미리보기용)
// ============================================================
router.get("/layout-summary/:screenId", getScreenLayoutSummary);
router.post("/layout-summary/batch", getMultipleScreenLayoutSummary);

// ============================================================
// 화면 서브 테이블 관계 (조인/참조 테이블)
// ============================================================
router.post("/sub-tables/batch", getScreenSubTables);

// ============================================================
// 메뉴-화면그룹 동기화
// ============================================================
// 동기화 상태 조회
router.get("/sync/status", getSyncStatusController);
// 화면관리 → 메뉴 동기화
router.post("/sync/screen-to-menu", syncScreenGroupsToMenuController);
// 메뉴 → 화면관리 동기화
router.post("/sync/menu-to-screen", syncMenuToScreenGroupsController);
// 전체 회사 동기화 (최고 관리자만)
router.post("/sync/all", syncAllCompaniesController);

// ============================================================
// POP 전용 화면 그룹 (hierarchy_path LIKE 'POP/%')
// ============================================================
router.get("/pop/groups", getPopScreenGroups);
router.post("/pop/groups", createPopScreenGroup);
router.put("/pop/groups/:id", updatePopScreenGroup);
router.delete("/pop/groups/:id", deletePopScreenGroup);
router.post("/pop/ensure-root", ensurePopRootGroup);

export default router;


