// HRM org/reference masters (docs/13-BACKEND-HRM-API.md §2, §5, §6). Each is the
// same list/get/create/update/status-toggle shape as brandsApi (see hrm-master-factory).
import { createMasterApi } from "@/lib/api/hrm-master-factory"
import {
  Branch,
  Department,
  Designation,
  EmploymentType,
  HrDocumentType,
  LeaveType,
  SalaryComponent,
  WorkShift,
} from "@/types/hrm"

export interface CreateBranchRequest { code: string; name: string; address?: string | null }
export type UpdateBranchRequest = Omit<CreateBranchRequest, "code">
export const branchesHrmApi = createMasterApi<Branch, CreateBranchRequest, UpdateBranchRequest>("branches")

export interface CreateDepartmentRequest {
  code: string
  name: string
  parentDepartmentId?: number | null
  headEmployeeId?: number | null
  branchId?: number | null
}
export type UpdateDepartmentRequest = Omit<CreateDepartmentRequest, "code">
export const departmentsApi = createMasterApi<Department, CreateDepartmentRequest, UpdateDepartmentRequest>("departments")

export interface CreateDesignationRequest { code: string; name: string }
export type UpdateDesignationRequest = Omit<CreateDesignationRequest, "code">
export const designationsApi = createMasterApi<Designation, CreateDesignationRequest, UpdateDesignationRequest>("designations")

export interface CreateEmploymentTypeRequest { code: string; name: string }
export type UpdateEmploymentTypeRequest = Omit<CreateEmploymentTypeRequest, "code">
export const employmentTypesApi = createMasterApi<EmploymentType, CreateEmploymentTypeRequest, UpdateEmploymentTypeRequest>("employment-types")

export interface CreateWorkShiftRequest {
  code: string
  name: string
  startTime: string
  endTime: string
  isOvernight: boolean
  graceMinutes: number
  breakMinutes: number
  standardWorkingMinutes: number
  otMultiplier: number
  workingDaysMask: number
}
export type UpdateWorkShiftRequest = Omit<CreateWorkShiftRequest, "code">
export const workShiftsApi = createMasterApi<WorkShift, CreateWorkShiftRequest, UpdateWorkShiftRequest>("work-shifts")

export interface CreateHrDocumentTypeRequest {
  code: string
  name: string
  category: HrDocumentType["category"]
  requiredAtOnboarding: boolean
  expiryTracked: boolean
}
export type UpdateHrDocumentTypeRequest = Omit<CreateHrDocumentTypeRequest, "code">
export const hrDocumentTypesApi = createMasterApi<HrDocumentType, CreateHrDocumentTypeRequest, UpdateHrDocumentTypeRequest>("hr-document-types")

export interface CreateLeaveTypeRequest {
  code: string
  name: string
  isPaid: boolean
  countsAsNoPay: boolean
  accrualPerYear: number
  carryForwardAllowed: boolean
  maxCarryForwardDays?: number | null
  requiresApproval: boolean
}
export type UpdateLeaveTypeRequest = Omit<CreateLeaveTypeRequest, "code">
export const leaveTypesApi = createMasterApi<LeaveType, CreateLeaveTypeRequest, UpdateLeaveTypeRequest>("leave-types")

export interface CreateSalaryComponentRequest {
  code: string
  name: string
  componentType: SalaryComponent["componentType"]
  isTaxable: boolean
  isEpfEtfApplicable: boolean
}
export type UpdateSalaryComponentRequest = Omit<CreateSalaryComponentRequest, "code">
export const salaryComponentsApi = createMasterApi<SalaryComponent, CreateSalaryComponentRequest, UpdateSalaryComponentRequest>("salary-components")
