import type { AdminRoleRow, AdminUserPermissionRow } from "@/types/api/admin-user";

export type AdminAgentContext = {
  id: number;
  admin_site_id: number;
  /** 主站名称（admin_sites.name），展示用 */
  admin_site_name?: string;
  /** 主站编号（admin_sites.code），创建玩家时预填 */
  site_code: string;
  path: string;
  code: string;
  name: string;
  depth: number;
  can_create_child_agent: boolean;
  can_create_player: boolean;
};

export type AgentNodeProfileSummary = {
  total_share_rate: number;
  credit_limit: number;
  allocated_credit: number;
  used_credit: number;
  available_credit: number;
  rebate_limit: number;
  default_player_rebate: number;
};

export type AgentNodeRow = {
  id: number;
  admin_site_id: number;
  site_code?: string | null;
  parent_id: number | null;
  path: string;
  depth: number;
  code: string;
  name: string;
  status: number;
  is_root: boolean;
  username?: string | null;
  email?: string | null;
  profile_summary?: AgentNodeProfileSummary | null;
  children?: AgentNodeRow[];
};

export type AgentTreeData = {
  admin_site_id: number;
  tree: AgentNodeRow[];
};

export type AgentNodeListData = {
  admin_site_id: number | null;
  items: AgentNodeRow[];
};

export type AgentProfilePayload = {
  relative_share_rate?: number;
  total_share_rate?: number;
  credit_limit?: number;
  rebate_limit?: number;
  default_player_rebate?: number;
  can_grant_extra_rebate?: boolean;
  can_create_child_agent?: boolean;
  can_create_player?: boolean;
  risk_tags?: string[];
};

export type AgentParentCaps = {
  agent_node_id: number;
  total_share_rate: number;
  rebate_limit: number;
  available_credit: number;
};

export type AgentProfileRow = AgentProfilePayload & {
  agent_node_id: number;
  relative_share_rate?: number | null;
  allocated_credit: number;
  used_credit: number;
  available_credit: number;
  parent_caps?: AgentParentCaps | null;
  risk_tags?: string[];
};

export type AgentNodeCreatePayload = {
  parent_id: number;
  code?: string | null;
  name: string;
  username: string;
  password: string;
  email?: string | null;
  status?: number;
} & AgentProfilePayload;

export type AgentNodeUpdatePayload = {
  name?: string;
  username?: string;
  password?: string;
  email?: string | null;
  status?: number;
};

export type AgentRoleListData = {
  agent_node_id: number;
  items: AdminRoleRow[];
};

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

export type AgentAdminUserListData = {
  agent_node_id: number;
  items: AdminUserPermissionRow[];
};

export type AgentAdminUserCreatePayload = {
  username: string;
  nickname: string;
  email?: string | null;
  password: string;
  status?: number;
  role_ids?: number[];
};

export type AgentAdminUserRoleSyncPayload = {
  role_ids: number[];
};

export type AgentDelegationGrantRow = {
  menu_action_id: number;
  permission_code: string;
  name: string;
  can_delegate: boolean;
};

export type AgentDelegationGrantsData = {
  child_agent_id: number;
  grants: AgentDelegationGrantRow[];
};

export type AgentDelegationGrantSyncPayload = {
  grants: Array<{
    menu_action_id: number;
    can_delegate?: boolean;
  }>;
};
