export interface MenuItem {
  code: string;
  name: string;
  path: string;
}

export type UserRole = "superuser" | "manager" | "viewer";

export interface UserProfile {
  id: number;
  username: string;
  display_name: string;
  avatar_url: string;
  email: string;
  phone: string;
  department: string;
  position_title: string;
  role: UserRole;
  is_active: boolean;
  menus: MenuItem[];
  permissions: string[];
}

export interface UserRecord extends UserProfile {
  created_at: string;
  updated_at: string;
}

export interface LoginResponse {
  access_token: string;
  token_type: string;
  user: UserProfile;
  menus: MenuItem[];
  permissions: string[];
}

export interface CreateUserRequest {
  username: string;
  password: string;
  role: UserRole;
  display_name: string;
  email: string;
  phone: string;
  department: string;
  position_title: string;
}

export interface UpdateProfileRequest {
  display_name: string;
  email: string;
  phone: string;
  department: string;
  position_title: string;
}

export interface ChangePasswordRequest {
  old_password: string;
  new_password: string;
  confirm_password: string;
}

export interface ChangePasswordResponse {
  message: string;
}

export interface PayrollResult {
  name: string;
  department: string;
  position: string;
  attendance_group: string;
  salary_month: string;
  salary_mode: string;
  attendance_days: number;
  absence_days: number;
  actual_work_hours: number;
  punch_overtime_hours: number;
  leave_hours: number;
  remaining_overtime_hours: number;
  workday_overtime_hours: number;
  weekend_overtime_hours: number;
  holiday_overtime_hours: number;
  minor_late_count: number;
  major_late_count: number;
  penalized_late_count: number;
  late_deduction: number;
  missing_card_count: number;
  missing_card_deduction: number;
  total_deduction: number;
  base_salary: number | null;
  base_pay: number | null;
  commission_amount: number;
  overtime_rate: number;
  weekend_overtime_rate: number;
  holiday_overtime_rate: number;
  overtime_pay: number;
  gross_salary: number | null;
  net_salary: number | null;
  note: string;
}

export interface PayrollResponse {
  job_id: string;
  employee_count: number;
  output_file: string | null;
  download_url: string | null;
  results: PayrollResult[];
}

export interface DingTalkPayrollRequest {
  user_ids: string[];
  start_date: string;
  end_date: string;
  config_path?: string | null;
  export_excel: boolean;
}

export interface RecentPayrollJob {
  job_id: string;
  source_type: string;
  employee_count: number;
  output_file: string | null;
  download_url: string | null;
  created_at: string;
}

export interface OperationLog {
  id: number;
  user_id: number | null;
  username: string;
  role: string;
  module: string;
  action: string;
  target_type: string;
  target_id: string;
  status: string;
  detail: string;
  ip_address: string;
  user_agent: string;
  created_at: string;
}

export interface OperationLogListResponse {
  items: OperationLog[];
  total: number;
  page: number;
  page_size: number;
}

export interface OperationLogQuery {
  page?: number;
  page_size?: number;
  username?: string;
  module?: string;
  action?: string;
  status?: string;
  keyword?: string;
  start_time?: string;
  end_time?: string;
}

export type EmploymentStatus = "active" | "inactive";

export interface EmployeeRecord {
  id: number;
  employee_no: string;
  name: string;
  dingtalk_user_id: string;
  department: string;
  position: string;
  phone: string;
  employment_status: EmploymentStatus;
  remark: string;
  created_at: string;
  updated_at: string;
}

export interface EmployeeListResponse {
  items: EmployeeRecord[];
  total: number;
  page: number;
  page_size: number;
}

export interface EmployeeNoResponse {
  employee_no: string;
}

export interface EmployeeRequest {
  employee_no: string;
  name: string;
  dingtalk_user_id: string;
  department: string;
  position: string;
  phone: string;
  employment_status: EmploymentStatus;
  remark: string;
}

export interface DepartmentRecord {
  id: number;
  parent_id: number | null;
  department_code: string;
  name: string;
  sort_order: number;
  is_active: boolean;
  remark: string;
  created_at: string;
  updated_at: string;
}

export interface DepartmentRequest {
  parent_id: number | null;
  department_code: string;
  name: string;
  sort_order: number;
  is_active: boolean;
  remark: string;
}

export interface PositionRecord {
  id: number;
  department_id: number;
  department: DepartmentRecord;
  position_code: string;
  name: string;
  sort_order: number;
  is_active: boolean;
  remark: string;
  created_at: string;
  updated_at: string;
}

export interface PositionRequest {
  department_id: number;
  position_code?: string;
  name: string;
  sort_order: number;
  is_active: boolean;
  remark: string;
}

export interface SalaryProfileRecord {
  id: number;
  employee_id: number;
  employee: EmployeeRecord;
  salary_mode: string;
  base_salary: number;
  hourly_rate: number;
  piece_quantity: number;
  piece_unit_price: number;
  probation_type: string;
  probation_ratio: number;
  probation_salary: number;
  commission_amount: number;
  overtime_rate: number;
  weekend_overtime_rate: number;
  holiday_overtime_rate: number;
  effective_month: string;
  remark: string;
  created_at: string;
  updated_at: string;
}

export interface SalaryProfileRequest {
  employee_id: number;
  salary_mode: string;
  base_salary: number;
  hourly_rate: number;
  piece_quantity: number;
  piece_unit_price: number;
  probation_type: string;
  probation_ratio: number;
  probation_salary: number;
  commission_amount: number;
  overtime_rate: number;
  weekend_overtime_rate: number;
  holiday_overtime_rate: number;
  effective_month: string;
  remark: string;
}

export interface CommissionRecord {
  id: number;
  employee_id: number;
  employee: EmployeeRecord;
  commission_month: string;
  commission_type: string;
  amount: number;
  source_type: string;
  business_ref: string;
  remark: string;
  created_at: string;
  updated_at: string;
}

export interface CommissionListResponse {
  items: CommissionRecord[];
  total: number;
  page: number;
  page_size: number;
}

export interface CommissionRecordRequest {
  employee_id: number;
  commission_month: string;
  commission_type: string;
  amount: number;
  source_type: string;
  business_ref: string;
  remark: string;
}

export interface CommissionImportResponse {
  imported_count: number;
  message: string;
}

export interface SalaryConfigRecord {
  id: number;
  config_key: string;
  config_name: string;
  config_group: string;
  config_value: string;
  value_type: string;
  is_enabled: boolean;
  remark: string;
  created_at: string;
  updated_at: string;
}

export interface SalaryConfigRequest {
  config_name: string;
  config_group: string;
  config_value: string;
  value_type: string;
  is_enabled: boolean;
  remark: string;
}

export interface PayrollReportResponse {
  salary_month: string | null;
  totals: Record<string, number>;
  attendance: Record<string, number>;
  departments: Array<{
    department: string;
    employee_count: number;
    gross_salary: number;
    net_salary: number;
  }>;
  recent_records: Array<{
    id: number;
    employee_no: string;
    employee_name: string;
    department: string;
    salary_month: string;
    salary_mode: string;
    base_pay: number;
    commission_amount: number;
    overtime_pay: number;
    deduction_amount: number;
    gross_salary: number;
    net_salary: number;
    created_at: string;
  }>;
}

export interface RealtimeAttendanceSummary {
  attendance_date: string;
  employee_total: number;
  checked_in_count: number;
  unchecked_count: number;
  late_count: number;
  leave_count: number;
  missing_card_count: number;
  estimated_overtime_count: number;
  estimated_net_total: number;
}

export interface TodayAttendanceItem {
  employee_id: number | null;
  employee_no: string;
  employee_name: string;
  department: string;
  position: string;
  first_punch: string;
  last_punch: string;
  attendance_status: string;
  late_minutes: number;
  missing_card_count: number;
  leave_hours: number;
  today_overtime_hours: number;
  month_remaining_overtime_hours: number;
  estimated_net_salary: number | null;
  note: string;
}

export interface RealtimeAttendanceResponse {
  sync_job_id: string | null;
  synced_at: string | null;
  summary: RealtimeAttendanceSummary;
  items: TodayAttendanceItem[];
}

export interface MonthlyPayrollRun {
  id: number;
  salary_month: string;
  source_type: string;
  source_job_id: string;
  status: "draft" | "calculated" | "reviewed" | "locked" | "failed" | string;
  employee_count: number;
  gross_total: number;
  net_total: number;
  deduction_total: number;
  overtime_total: number;
  locked_by: string;
  locked_at: string | null;
  created_by: string;
  created_at: string;
  updated_at: string;
}

export interface PayrollException {
  id: number;
  run_id: number | null;
  sync_job_id: string | null;
  employee_id: number | null;
  employee_no: string;
  employee_name: string;
  salary_month: string;
  exception_type: string;
  severity: string;
  message: string;
  status: string;
  created_at: string;
  resolved_at: string | null;
}

export interface MonthlyPayrollDetailResponse {
  run: MonthlyPayrollRun;
  results: PayrollResult[];
  exceptions: PayrollException[];
}
