export interface UserName {
  am: string;
  en: string;
}

export interface Role {
  id: string;
  key: string;
}

export interface Permission {
  id: string;
  key: string;
}
export interface PositionTypePermissionDto {
  id: string;
  permissionId: string;
  positionTypeId: string;
  createdAt: string;
  updatedAt: string;
  permission: {
    id: string;
    key: string;
    name: {
      am: string;
      en: string;
    };
    createdAt: string;
    updatedAt: string;
  };
}

export interface PositionTypeDto {
  id: string;
  key: string;
  name: {
    am: string;
    en: string;
  };
  unitId: string;
  isSystem: boolean;
  createdAt: string;
  updatedAt: string;
  positionTypePermissions: PositionTypePermissionDto[];
}

export interface EmployeeAssociation {
  id: string;
  name: UserName;
  employeePositionId?: string;
  organizationId: string;
  unitId: string | null;
  positions: {
    id: string;
    employeePositionId: string;
    name: {
      am: string;
      en: string;
    };
    key: string;
    rank: number;
    projectId?: string | null;
    isDelegate: boolean;
    delegatorId: string | null;
    parentPositionId: string | null;
    permissions: Permission[];
    hasDelegated: boolean;
    hasPendingDelegation: boolean;
    positionType?: {
      id: string;
      key: string;
      name: { am: string; en: string };
      canReceiveRecord: boolean;
      canCreateBankRecord?: boolean;
      canAssignRecord: boolean;
    } | null;
    delegationDeadline: string | null;
    delegationStartDate: string | null;
  }[];
}
export enum EPositionTypeKey {
  EMPLOYEE = "employee",
  TEAM_LEADER = "team-leader",
  DIRECTOR = "director",
  DEPUTY = "deputy",
  OFFICE_HEAD = "office-head",
  RECORD_OFFICER = "record-officer",
  BUDGET_PLAN = "budget-and-plan",
}

// Aliases for position-type keys returned by the backend in different
// casings/separators. Used by the matcher in positionTypeUtils so the FE
// stays correct whether the backend returns "team-leader", "teamLeader",
// or "team_leader". Also covers org-specific role labels (e.g. "expert",
// "fetami-balmuya") that semantically map to one of the canonical types.
export const POSITION_TYPE_ALIASES: Record<EPositionTypeKey, string[]> = {
  [EPositionTypeKey.EMPLOYEE]: [
    "employee",
    "expert",
    "performance_expert",
    "performanceExpert",
    "fetami-balmuya",
    "fetamiBalmuya",
    "fetami_balmuya",
    "adwa_victory_vemorial_museum/fetami-balmuya",
    "adwa_victory_vemorial_museum/performance_expert",
  ],
  [EPositionTypeKey.TEAM_LEADER]: [
    "team-leader",
    "teamleader",
    "teamLeader",
    "team_leader",
    "performance_team_leader",
    "performance_team-leader",
    "performance_teamleader",
    "performanceTeamLeader",
    "Performance_teamleader",
  ],
  [EPositionTypeKey.DIRECTOR]: [
    "director",
    "performance_director",
    "performance-director",
    "performanceDirector",
    "Performance_Director",
    "adwa_victory_vemorial_museum/director",
  ],
  [EPositionTypeKey.DEPUTY]: ["deputy"],
  [EPositionTypeKey.OFFICE_HEAD]: ["office-head", "officeHead", "office_head"],
  [EPositionTypeKey.RECORD_OFFICER]: [
    "record-officer",
    "recordOfficer",
    "record_officer",
  ],
  [EPositionTypeKey.BUDGET_PLAN]: [
    "budget-and-plan",
    "budgetAndPlan",
    "budget_and_plan",
  ],
};

export interface MeDto {
  id: string;
  email: string;
  name: UserName;
  userType: "employee" | string;
  sharepointId: string | null;
  status: string;
  username: string;
  roles: Role[];
  permissions: Permission[];
  employee: EmployeeAssociation[];
  phoneNumber: string;
  hasSetPassword: boolean;
  hasFinishedRegistration: boolean;
  hasFinishedDMSOnboarding: boolean;
  mfaRequired?: boolean;
}

// Name interface
export interface Name {
  am: string;
  en: string;
}

// Permission interface
export interface Permission {
  id: string;
  key: string;
}

// Position interface
export interface Position {
  id: string;
  employeePositionId: string;
  name: Name;
  key: string;
  rank: number;
  isDelegate: boolean;
  projectId: string | null;
  delegatorId: string | null;
  parentPositionId: string | null;
  permissions: Permission[];
  hasDelegated: boolean;
  positionType: string | null;
  delegationDeadline: string | null;
  delegationStartDate: string | null;
}

// Employee interface
export interface Employee {
  id: string;
  organizationId: string;
  unitId: string;
  name: Name;
  positions: Position[];
}

// User interface (main DTO)
export interface UserDto {
  id: string;
  email: string;
  name: Name;
  userType: string;
  sharepointId: string | null;
  status: string;
  username: string;
  hasSetPassword: boolean;
  roles: string[];
  permissions: Permission[];
  employees: Employee[];
}
