import type { AdminNavItem } from "@/modules/_config/admin-nav";

export type AdminUserSiteBinding = {
  site_id: number;
  site_code: string;
  site_name: string;
  role_slugs: string[];
};

export type AdminUserPermissionRow = {
  id: number;
  username: string;
  nickname: string;
  email: string | null;
  status: number;
  account_kind?: "platform" | "agent";
  roles: string[];
  site_bindings?: AdminUserSiteBinding[];
  direct_permissions: string[];
  effective_permissions: string[];
};

export type AdminUserPermissionListData = {
  items: AdminUserPermissionRow[];
  meta: {
    current_page: number;
    per_page: number;
    total: number;
    last_page: number;
  };
};

export type AdminPermissionCatalogData = {
  permissions: { id: number; slug: string; name: string }[];
  /** 菜单/业务域 → 下属直接权限（prd.*），二级列表用 */
  permission_menu_groups?: {
    key: string;
    label: string;
    permissions: { id: number; slug: string; name: string }[];
  }[];
  navigation?: AdminNavItem[];
  roles: AdminRoleRow[];
};

export type AdminRoleRow = {
  id: number;
  slug: string;
  name: string;
  description: string | null;
  status: number;
  is_system: boolean;
  sort_order: number;
  scope_type?: string;
  owner_agent_id?: number | null;
  delegated_from_role_id?: number | null;
  is_read_only_template?: boolean;
  permission_slugs: string[];
  user_count: number;
};

export type AdminRoleListData = {
  items: AdminRoleRow[];
};

export type AdminRoleCreatePayload = {
  slug: string;
  name: string;
  description?: string | null;
  status?: number;
  permission_slugs?: string[];
};

export type AdminRoleUpdatePayload = {
  slug?: string;
  name?: string;
  description?: string | null;
  status?: number;
};

export type AdminRoleDeleteResult = {
  deleted: boolean;
  id: number;
};

export type AdminUserPermissionSyncData = {
  id: number;
  username: string;
  nickname: string;
  account_kind?: "platform" | "agent";
  roles: string[];
  site_bindings?: AdminUserSiteBinding[];
  direct_permissions: string[];
  effective_permissions: string[];
};

/** 与 {@link AdminUserPermissionSyncData} 相同（角色同步返回体一致） */
export type AdminUserRoleSyncData = AdminUserPermissionSyncData;

export type AdminUserCreatePayload = {
  username: string;
  nickname: string;
  email?: string | null;
  password: string;
  status?: number;
  admin_site_id: number;
  role_slugs: string[];
};

export type AdminUserRoleSyncPayload = {
  admin_site_id: number;
  role_slugs: string[];
};

export type AdminUserUpdatePayload = {
  nickname?: string;
  email?: string | null;
  password?: string;
  status?: number;
};

export type AdminUserDeleteResult = {
  deleted: boolean;
  id: number;
};
