// Enums
export enum OrganizationalLevel {
  Division = 'Division',  // 师团
  Regiment = 'Regiment',  // 团
  Battalion = 'Battalion', // 营
  Company = 'Company'    // 连
}

// 用于权限判断的级别数值映射
export const OrganizationalLevelValue: Record<OrganizationalLevel, number> = {
  [OrganizationalLevel.Division]: 1,
  [OrganizationalLevel.Regiment]: 2,
  [OrganizationalLevel.Battalion]: 3,
  [OrganizationalLevel.Company]: 4
}

export enum PersonnelLevel {
  Division = 'Division',  // 师级人才
  Regiment = 'Regiment',  // 团级人才
  Battalion = 'Battalion', // 营级人才
  Company = 'Company'    // 连级人才
}

export enum PersonnelStatus {
  Pending = 'Pending',
  Approved = 'Approved',
  Rejected = 'Rejected'
}

export enum ApprovalStatus {
  Pending = 'Pending',
  Approved = 'Approved',
  Rejected = 'Rejected'
}

export enum ApprovalRequestType {
  AllocationModification = 'AllocationModification',
  ReportModification = 'ReportModification',
  PersonnelModification = 'PersonnelModification'
}

// Auth types
export interface LoginRequest {
  username: string
  password: string
}

export interface LoginResponse {
  token: string
  userId: number
  username: string
  displayName: string
  organizationalUnitId: number
  organizationalUnitName: string
  organizationalLevel: OrganizationalLevel
}

export interface User {
  id: number
  username: string
  displayName: string
  organizationalUnitId: number
  organizationalUnitName: string
  organizationalLevel: OrganizationalLevel
}

// Organization types
export interface OrganizationalUnit {
  id: number
  name: string
  level: OrganizationalLevel
  parentId: number | null
  children?: OrganizationalUnit[]
  createdAt: string
}

export interface CreateOrganizationRequest {
  name: string
  level: OrganizationalLevel
  parentId: number | null
}

// Allocation types
export interface MaterialAllocation {
  id: number
  category: string
  materialName: string
  unit: string
  totalQuota: number
  createdByUnitId: number
  createdByUnitName: string
  distributions: AllocationDistribution[]
  createdAt: string
}

export interface AllocationDistribution {
  id: number
  allocationId: number
  targetUnitId: number
  targetUnitName: string
  unitQuota: number
  actualCompletion: number | null
  completionRate: number
  reportedAt: string | null
  reportedByUserId: number | null
}

export interface CreateAllocationRequest {
  category: string
  materialName: string
  unit: string
  totalQuota: number
  distributions: DistributionRequest[]
}

export interface DistributionRequest {
  targetUnitId: number
  unitQuota: number
}

export interface UpdateDistributionRequest {
  actualCompletion: number
  remarks?: string
}

// Report types
export interface ReportData {
  distributionId: number
  materialName: string
  category: string
  unit: string
  unitQuota: number
  actualCompletion: number | null
  completionRate: number
  targetUnitName: string
  reportedAt: string | null
}

export interface AggregatedReport {
  organizationalUnitId: number
  organizationalUnitName: string
  level: OrganizationalLevel
  totalQuota: number
  totalCompletion: number
  overallCompletionRate: number
  subordinateReports: AggregatedReport[]
}

export interface SubmitReportRequest {
  distributionId: number
  actualCompletion: number
}

// Personnel types
export interface Personnel {
  id: number
  name: string
  photoPath: string
  unit: string
  position: string
  rank: string
  gender: string
  idNumber: string
  professionalTitle: string
  educationLevel: string
  age: number
  height: number
  contactInfo: string
  hometown: string
  trainingParticipation: string
  achievements: string
  supportingDocuments: string
  ethnicity: string
  politicalStatus: string
  birthDate: string
  enlistmentDate: string
  specialty: string
  submittedByUnitId: number
  submittedByUnitName: string
  approvedLevel: PersonnelLevel | null
  approvedByUnitId: number | null
  approvedByUnitName: string | null
  status: PersonnelStatus
  submittedAt: string
  approvedAt: string | null
  pendingUpgradeByUnitId: number | null // 待向上申报的申报单位ID
}

export interface CreatePersonnelRequest {
  name: string
  photo: File | null
  position: string
  rank: string
  gender: string
  idNumber: string
  professionalTitle: string
  educationLevel: string
  age: number
  height: number
  contactInfo: string
  hometown: string
  trainingParticipation: string
  achievements: string
  supportingDocuments: File | null
}

export interface PersonnelApprovalRequest {
  personnelId: number
  approved: boolean
  comments?: string
  level?: string
}

// Approval types
export interface ApprovalRequest {
  id: number
  type: ApprovalRequestType
  requestedByUserId: number
  requestedByUserName: string
  reason: string
  originalData: string
  requestedChanges: string
  status: ApprovalStatus
  reviewedByUserId: number | null
  reviewedByUserName: string | null
  reviewComments: string | null
  requestedAt: string
  reviewedAt: string | null
}

export interface CreateApprovalRequest {
  type: ApprovalRequestType
  reason: string
  originalData: string
  requestedChanges: string
  relatedEntityId: number
}

export interface ReviewApprovalRequest {
  approved: boolean
  comments?: string
}

// Pagination
export interface PaginatedResponse<T> {
  items: T[]
  totalCount: number
  pageNumber: number
  pageSize: number
  totalPages: number
}

export interface PaginationParams {
  pageNumber?: number
  pageSize?: number
}

// API Response
export interface ApiResponse<T> {
  success: boolean
  data?: T
  message?: string
  errors?: string[]
}
