package com.erp.provisioning;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 회사 프로비저닝 시 복사 가능한 테이블 그룹.
 * 그룹 id = enum 이름 (SCREEN 등). UI 체크박스와 1:1 매핑.
 * required=true 그룹은 UI 에서 해제 불가 (disabled checkbox).
 */
public enum TableGroup {

    SCREEN("화면관리", true, List.of(
            "screen_definitions", "screen_groups", "screen_group_screens",
            "screen_layouts_v1", "screen_layouts_v2", "screen_layouts_v3",
            "screen_layouts_pop", "screen_menu_assignments",
            "screen_conditional_zones", "screen_data_flows", "screen_data_transfer",
            "screen_embedding", "screen_field_joins", "screen_split_panel",
            "screen_table_relations", "screen_templates"
    )),
    CONTROL("제어관리", true, List.of("dtg_management")),
    BATCH("배치관리", true, List.of("batch_configs", "batch_mappings", "batch_execution_logs")),
    DASHBOARD("대시보드", false, List.of("dashboard_cards", "dashboard_elements")),
    DATAFLOW("데이터플로우", false, List.of("dataflow_diagrams", "node_flows")),
    MENU_AUTH("권한/메뉴 기본값", false, List.of(
            "menu_info", "authority_master", "authority_master_history",
            "authority_sub_menu", "authority_sub_user"
    ));

    private final String label;
    private final boolean required;
    private final List<String> tables;

    TableGroup(String label, boolean required, List<String> tables) {
        this.label = label;
        this.required = required;
        this.tables = tables;
    }

    public String getLabel() { return label; }
    public boolean isRequired() { return required; }
    public List<String> getTables() { return tables; }

    /** API 응답용 Map 변환. UI 에서 체크박스 렌더에 필요한 모든 정보. */
    public Map<String, Object> toMap() {
        return Map.of(
                "id", name(),
                "label", label,
                "required", required,
                "tables", tables
        );
    }

    public static TableGroup parse(String id) {
        try {
            return TableGroup.valueOf(id);
        } catch (IllegalArgumentException e) {
            return null;
        }
    }

    public static List<TableGroup> requiredGroups() {
        return Arrays.stream(values()).filter(TableGroup::isRequired).toList();
    }
}
