from __future__ import annotations

from dataclasses import dataclass


ROLE_SUPERUSER = "superuser"
ROLE_MANAGER = "manager"
ROLE_VIEWER = "viewer"
VALID_ROLES = {ROLE_SUPERUSER, ROLE_MANAGER, ROLE_VIEWER}

PERMISSION_PAYROLL_EXCEL_CALCULATE = "payroll:excel:calculate"
PERMISSION_PAYROLL_DINGTALK_CALCULATE = "payroll:dingtalk:calculate"
PERMISSION_PAYROLL_JOB_VIEW = "payroll:job:view"
PERMISSION_PAYROLL_FILE_DOWNLOAD = "payroll:file:download"
PERMISSION_ATTENDANCE_REALTIME_VIEW = "attendance:realtime:view"
PERMISSION_ATTENDANCE_REALTIME_SYNC = "attendance:realtime:sync"
PERMISSION_MONTHLY_PAYROLL_VIEW = "monthly_payroll:view"
PERMISSION_MONTHLY_PAYROLL_CALCULATE = "monthly_payroll:calculate"
PERMISSION_MONTHLY_PAYROLL_LOCK = "monthly_payroll:lock"
PERMISSION_MONTHLY_PAYROLL_EXPORT = "monthly_payroll:export"
PERMISSION_USER_CREATE = "user:create"
PERMISSION_USER_LIST = "user:list"
PERMISSION_OPERATION_LOG_VIEW = "operation_log:view"
PERMISSION_EMPLOYEE_VIEW = "employee:view"
PERMISSION_EMPLOYEE_MANAGE = "employee:manage"
PERMISSION_ORGANIZATION_VIEW = "organization:view"
PERMISSION_ORGANIZATION_MANAGE = "organization:manage"
PERMISSION_SALARY_PROFILE_VIEW = "salary_profile:view"
PERMISSION_SALARY_PROFILE_MANAGE = "salary_profile:manage"
PERMISSION_COMMISSION_VIEW = "commission:view"
PERMISSION_COMMISSION_MANAGE = "commission:manage"
PERMISSION_CONFIG_VIEW = "config:view"
PERMISSION_CONFIG_MANAGE = "config:manage"
PERMISSION_REPORT_VIEW = "report:view"


@dataclass(frozen=True)
class MenuPermission:
    """前端菜单权限描述。"""

    code: str
    name: str
    path: str


MENUS = {
    "attendance_realtime": MenuPermission(code="attendance_realtime", name="实时考勤", path="/attendance/realtime"),
    "monthly_payroll": MenuPermission(code="monthly_payroll", name="月度核算", path="/payroll/monthly"),
    "payroll": MenuPermission(code="payroll", name="工资计算", path="/payroll"),
    "jobs": MenuPermission(code="jobs", name="计算记录", path="/payroll/jobs"),
    "employees": MenuPermission(code="employees", name="员工维护", path="/system/employees"),
    "organization": MenuPermission(code="organization", name="组织架构", path="/system/organization"),
    "salary_profiles": MenuPermission(code="salary_profiles", name="薪资维护", path="/system/salary-profiles"),
    "commissions": MenuPermission(code="commissions", name="提成管理", path="/payroll/commissions"),
    "reports": MenuPermission(code="reports", name="统计报表", path="/reports"),
    "configs": MenuPermission(code="configs", name="规则配置", path="/system/configs"),
    "users": MenuPermission(code="users", name="用户管理", path="/system/users"),
    "operation_logs": MenuPermission(code="operation_logs", name="操作日志", path="/system/operation-logs"),
}

ROLE_MENU_CODES = {
    ROLE_SUPERUSER: (
        "attendance_realtime",
        "monthly_payroll",
        "employees",
        "organization",
        "salary_profiles",
        "commissions",
        "reports",
        "configs",
        "users",
        "operation_logs",
    ),
    ROLE_MANAGER: (
        "attendance_realtime",
        "monthly_payroll",
        "employees",
        "organization",
        "salary_profiles",
        "commissions",
        "reports",
        "configs",
        "operation_logs",
    ),
    ROLE_VIEWER: ("attendance_realtime", "monthly_payroll", "reports"),
}

ROLE_OPERATION_PERMISSIONS = {
    ROLE_SUPERUSER: (
        PERMISSION_ATTENDANCE_REALTIME_VIEW,
        PERMISSION_ATTENDANCE_REALTIME_SYNC,
        PERMISSION_MONTHLY_PAYROLL_VIEW,
        PERMISSION_MONTHLY_PAYROLL_CALCULATE,
        PERMISSION_MONTHLY_PAYROLL_LOCK,
        PERMISSION_MONTHLY_PAYROLL_EXPORT,
        PERMISSION_PAYROLL_EXCEL_CALCULATE,
        PERMISSION_PAYROLL_DINGTALK_CALCULATE,
        PERMISSION_PAYROLL_JOB_VIEW,
        PERMISSION_PAYROLL_FILE_DOWNLOAD,
        PERMISSION_USER_CREATE,
        PERMISSION_USER_LIST,
        PERMISSION_OPERATION_LOG_VIEW,
        PERMISSION_EMPLOYEE_VIEW,
        PERMISSION_EMPLOYEE_MANAGE,
        PERMISSION_ORGANIZATION_VIEW,
        PERMISSION_ORGANIZATION_MANAGE,
        PERMISSION_SALARY_PROFILE_VIEW,
        PERMISSION_SALARY_PROFILE_MANAGE,
        PERMISSION_COMMISSION_VIEW,
        PERMISSION_COMMISSION_MANAGE,
        PERMISSION_CONFIG_VIEW,
        PERMISSION_CONFIG_MANAGE,
        PERMISSION_REPORT_VIEW,
    ),
    ROLE_MANAGER: (
        PERMISSION_ATTENDANCE_REALTIME_VIEW,
        PERMISSION_ATTENDANCE_REALTIME_SYNC,
        PERMISSION_MONTHLY_PAYROLL_VIEW,
        PERMISSION_MONTHLY_PAYROLL_CALCULATE,
        PERMISSION_MONTHLY_PAYROLL_LOCK,
        PERMISSION_MONTHLY_PAYROLL_EXPORT,
        PERMISSION_PAYROLL_EXCEL_CALCULATE,
        PERMISSION_PAYROLL_DINGTALK_CALCULATE,
        PERMISSION_PAYROLL_JOB_VIEW,
        PERMISSION_PAYROLL_FILE_DOWNLOAD,
        PERMISSION_OPERATION_LOG_VIEW,
        PERMISSION_EMPLOYEE_VIEW,
        PERMISSION_EMPLOYEE_MANAGE,
        PERMISSION_ORGANIZATION_VIEW,
        PERMISSION_ORGANIZATION_MANAGE,
        PERMISSION_SALARY_PROFILE_VIEW,
        PERMISSION_SALARY_PROFILE_MANAGE,
        PERMISSION_COMMISSION_VIEW,
        PERMISSION_COMMISSION_MANAGE,
        PERMISSION_CONFIG_VIEW,
        PERMISSION_CONFIG_MANAGE,
        PERMISSION_REPORT_VIEW,
    ),
    ROLE_VIEWER: (
        PERMISSION_ATTENDANCE_REALTIME_VIEW,
        PERMISSION_PAYROLL_JOB_VIEW,
        PERMISSION_PAYROLL_FILE_DOWNLOAD,
        PERMISSION_REPORT_VIEW,
    ),
}


def menus_for_role(role: str) -> list[MenuPermission]:
    """返回角色可见菜单。"""
    return [MENUS[code] for code in ROLE_MENU_CODES.get(role, ())]


def permissions_for_role(role: str) -> list[str]:
    """返回角色可执行的操作权限码。"""
    return list(ROLE_OPERATION_PERMISSIONS.get(role, ()))


def has_permission(role: str, permission: str) -> bool:
    """路由鉴权使用的权限判断。"""
    return permission in ROLE_OPERATION_PERMISSIONS.get(role, ())
