// ============================================================
// EnumResolver — 统一枚举解析器（Facade Pattern）
//
// 聚合所有业务模块的 LabelMap/ColorMap，提供类型安全的
// 字典查询 + 运行时兜底。
//
// 设计意图：
//   - 单一入口：组件只需 import EnumResolver，无需分别导入各模块
//   - 类型安全：label/color 参数为具体枚举字面量，编译时阻止拼写错误
//   - 运行时兜底：通过 || value || '默认文案' 处理后端新增枚举值
//
// 设计模式：
//   Facade（外观）  — 屏蔽 7 个模块的复杂性，暴露统一 API
//   Namespace Object — 按业务域二级分组
//   Strategy        — 每个 leaf 是可替换的解析策略
//
// 使用示例：
//   EnumResolver.billing.entryType.label('task_hold')   // "任务消耗"
//   EnumResolver.auth.userRole.label('admin')           // "管理账户"
//   EnumResolver.announcement.type.color('feature')     // "purple"
// ============================================================

import {
    PAYMENT_STATUS_TEXT_MAP,
    type PaymentStatus,
} from '@/modules/account/controllers/payment-api';

import {
    EntryTypeLabelMap,
    EntryTypeColorMap,
    type EntryTypeValue,
} from '@/modules/account/controllers/billing-api';

import {
    UserRoleTypeLabelMap,
    UserAccountTypeLabelMap,
    type UserRoleTypeValue,
    type UserAccountTypeValue,
} from '@/modules/auth/controllers/auth-api';

import {
    VipOrderStatusLabelMap,
    TargetAccountTypeLabelMap,
    type VipOrderStatusValue,
    type TargetAccountTypeValue,
} from '@/modules/account/controllers/vip-api';

import {
    OutputTypeMap,
    TaskStatusMap,
    type OutputTypeString,
    type TaskStatusString,
} from '@/modules/home/controllers/task-api';

import {
    BannerCategoryLabelMap,
    type BannerCategoryValue,
} from '@/modules/home/controllers/banner-api';

import {
    AnnouncementTypeLabelMap,
    AnnouncementTypeColorMap,
    type AnnouncementTypeValue,
} from '@/modules/announcement/controllers/announcement-api';

import {
    MODEL_CATEGORY_LABELS,
    CATEGORY_SLUG_MAP,
    type ModelCategoryStringEnum,
} from '@/modules/home/controllers/model-api';

// ============================================================
// 模型分类 slug → 前端标识反向映射
// ============================================================

const SLUG_TO_CATEGORY: Record<string, string> = Object.fromEntries(
    Object.entries(CATEGORY_SLUG_MAP).map(([cat, slug]) => [slug as string, cat]),
);

// ============================================================
// EnumResolver
// ============================================================

export const EnumResolver = {
    /** 支付 */
    payments: {
        status: {
            label: (value: PaymentStatus) =>
                PAYMENT_STATUS_TEXT_MAP[value] || value || '未知状态',
            color: (value: PaymentStatus) => {
                if (value === 'pending') return 'processing';
                if (value === 'paid') return 'success';
                if (value === 'failed') return 'error';
                return 'default';
            },
        },
        method: {
            label: (value: string) => {
                if (value === 'alipay') return '支付宝';
                if (value === 'wechat') return '微信支付';
                return value || '—';
            },
        },
    },

    /** 账单 */
    billing: {
        entryType: {
            label: (value: EntryTypeValue) =>
                EntryTypeLabelMap[value] || value || '未知账单类型',
            color: (value: EntryTypeValue) =>
                EntryTypeColorMap[value] || 'default',
        },
    },

    /** 用户身份 */
    auth: {
        userRole: {
            label: (value: UserRoleTypeValue) =>
                UserRoleTypeLabelMap[value] || value || '未知角色',
        },
        accountType: {
            label: (value: UserAccountTypeValue) =>
                UserAccountTypeLabelMap[value] || value || '未知账户类型',
        },
    },

    /** 任务 */
    task: {
        outputType: {
            label: (value: OutputTypeString) =>
                OutputTypeMap[value] || value || '未知输出类型',
        },
        status: {
            label: (value: TaskStatusString) =>
                TaskStatusMap[value] || value || '未知任务状态',
        },
    },

    /** VIP */
    vip: {
        orderStatus: {
            label: (value: VipOrderStatusValue) =>
                VipOrderStatusLabelMap[value] || value || '未知订单状态',
            color: (value: VipOrderStatusValue) => {
                if (value === 'pending') return 'processing';
                if (value === 'paid') return 'success';
                if (value === 'cancelled') return 'error';
                return 'default';
            },
        },
        targetAccountType: {
            label: (value: TargetAccountTypeValue) =>
                TargetAccountTypeLabelMap[value] || value || '未知账户类型',
        },
    },

    /** Banner */
    banner: {
        category: {
            label: (value: BannerCategoryValue) =>
                BannerCategoryLabelMap[value] || value || '未知分类',
        },
    },

    /** 公告 */
    announcement: {
        type: {
            label: (value: AnnouncementTypeValue) =>
                AnnouncementTypeLabelMap[value] || value || '未知公告类型',
            color: (value: AnnouncementTypeValue) =>
                AnnouncementTypeColorMap[value] || 'default',
        },
    },

    /** 模型分类（两步解析：前端字面量 > 后端 slug 反向映射 > 兜底） */
    model: {
        category: {
            label: (value: ModelCategoryStringEnum) =>
                MODEL_CATEGORY_LABELS[value] ||
                MODEL_CATEGORY_LABELS[SLUG_TO_CATEGORY[value] as ModelCategoryStringEnum] ||
                value,
        },
    },
};
