import { RewardType } from './reward.types';

/**
 * Payroll period types
 */
export enum PayrollPeriod {
  WEEKLY = 'weekly',
  BIWEEKLY = 'biweekly',
  MONTHLY = 'monthly',
  QUARTERLY = 'quarterly'
}

/**
 * Payroll status
 */
export enum PayrollStatus {
  DRAFT = 'draft',
  CALCULATING = 'calculating',
  PENDING_APPROVAL = 'pending_approval',
  APPROVED = 'approved',
  PROCESSING = 'processing',
  COMPLETED = 'completed',
  FAILED = 'failed'
}

/**
 * Payroll entry interface
 */
export interface PayrollEntry {
  id: string;
  userId: string;
  payrollId: string;
  rewardBreakdown: PayrollRewardBreakdown[];
  totalAmount: number;
  currency: string;
  status: PayrollStatus;
  calculatedAt: Date;
  approvedAt?: Date;
  paidAt?: Date;
  approvedBy?: string;
  notes?: string;
}

/**
 * Payroll reward breakdown
 */
export interface PayrollRewardBreakdown {
  rewardType: RewardType;
  count: number;
  totalAmount: number;
  averageAmount: number;
  rewardIds: string[];
}

/**
 * Payroll batch interface
 */
export interface PayrollBatch {
  id: string;
  period: PayrollPeriod;
  startDate: Date;
  endDate: Date;
  status: PayrollStatus;
  totalParticipants: number;
  totalAmount: number;
  currency: string;
  entries: PayrollEntry[];
  createdAt: Date;
  updatedAt: Date;
  calculatedAt?: Date;
  approvedAt?: Date;
  completedAt?: Date;
  createdBy: string;
  approvedBy?: string;
  notes?: string;
}

/**
 * Payroll calculation input
 */
export interface PayrollCalculationInput {
  period: PayrollPeriod;
  startDate: Date;
  endDate: Date;
  userIds?: string[]; // If empty, calculate for all users
  includeRewardTypes?: RewardType[];
  excludeRewardTypes?: RewardType[];
}

/**
 * Payroll calculation result
 */
export interface PayrollCalculationResult {
  batchId: string;
  totalParticipants: number;
  totalAmount: number;
  currency: string;
  entries: PayrollEntry[];
  summary: PayrollSummary;
}

/**
 * Payroll summary
 */
export interface PayrollSummary {
  totalAmount: number;
  participantCount: number;
  averageAmount: number;
  rewardTypeBreakdown: Array<{
    type: RewardType;
    count: number;
    totalAmount: number;
    participantCount: number;
  }>;
  topEarners: Array<{
    userId: string;
    userName: string;
    amount: number;
  }>;
}

/**
 * Payroll filter options
 */
export interface PayrollFilter {
  period?: PayrollPeriod;
  status?: PayrollStatus;
  startDate?: Date;
  endDate?: Date;
  userId?: string;
  createdBy?: string;
  limit?: number;
  offset?: number;
}

/**
 * Payroll configuration
 */
export interface PayrollConfig {
  defaultPeriod: PayrollPeriod;
  autoCalculation: boolean;
  requireApproval: boolean;
  currency: string;
  minimumAmount: number;
  processingDelay: number; // in hours
  notifications: {
    calculationComplete: boolean;
    approvalRequired: boolean;
    paymentProcessed: boolean;
  };
}

/**
 * Default payroll configuration
 */
export const defaultPayrollConfig: PayrollConfig = {
  defaultPeriod: PayrollPeriod.MONTHLY,
  autoCalculation: false,
  requireApproval: true,
  currency: 'IDR',
  minimumAmount: 10000, // IDR
  processingDelay: 24, // 24 hours
  notifications: {
    calculationComplete: true,
    approvalRequired: true,
    paymentProcessed: true
  }
};