import { IsString, IsOptional, IsNumber, IsUUID, IsDateString, IsArray, IsObject, MaxLength, IsEnum, Min } from 'class-validator';

export class CreateInvoiceDto {
  @IsEnum(['sale', 'purchase', 'credit_note', 'debit_note']) invoiceType: 'sale' | 'purchase' | 'credit_note' | 'debit_note';
  @IsUUID() partnerId: string;
  @IsOptional() @IsString() @MaxLength(200) partnerName?: string;
  @IsOptional() @IsString() @MaxLength(50) partnerTaxId?: string;
  @IsOptional() @IsUUID() salesOrderId?: string;
  @IsOptional() @IsUUID() purchaseOrderId?: string;
  @IsOptional() @IsObject() billingAddress?: object;
  @IsOptional() @IsDateString() invoiceDate?: string;
  @IsOptional() @IsDateString() dueDate?: string;
  @IsOptional() @IsString() @MaxLength(3) currency?: string;
  @IsOptional() @IsNumber() exchangeRate?: number;
  @IsOptional() @IsNumber() paymentTermDays?: number;
  @IsOptional() @IsString() paymentMethod?: string;
  @IsOptional() @IsString() notes?: string;
  @IsOptional() @IsArray() items?: CreateInvoiceItemDto[];
}

export class CreateInvoiceItemDto {
  @IsOptional() @IsUUID() productId?: string;
  @IsString() @MaxLength(200) productName: string;
  @IsOptional() @IsString() @MaxLength(50) productSku?: string;
  @IsOptional() @IsString() @MaxLength(20) satProductCode?: string;
  @IsOptional() @IsString() @MaxLength(10) satUnitCode?: string;
  @IsNumber() @Min(0) quantity: number;
  @IsOptional() @IsString() @MaxLength(20) uom?: string;
  @IsNumber() @Min(0) unitPrice: number;
  @IsOptional() @IsNumber() discountPercent?: number;
  @IsOptional() @IsNumber() taxRate?: number;
}

export class UpdateInvoiceDto {
  @IsOptional() @IsDateString() dueDate?: string;
  @IsOptional() @IsString() paymentMethod?: string;
  @IsOptional() @IsString() notes?: string;
  @IsOptional() @IsEnum(['draft', 'validated', 'sent', 'partial', 'paid', 'cancelled', 'voided']) status?: string;
}

export class CreatePaymentDto {
  @IsEnum(['received', 'made']) paymentType: 'received' | 'made';
  @IsUUID() partnerId: string;
  @IsOptional() @IsString() @MaxLength(200) partnerName?: string;
  @IsNumber() @Min(0) amount: number;
  @IsOptional() @IsString() @MaxLength(3) currency?: string;
  @IsOptional() @IsNumber() exchangeRate?: number;
  @IsOptional() @IsDateString() paymentDate?: string;
  @IsString() @MaxLength(50) paymentMethod: string;
  @IsOptional() @IsString() @MaxLength(100) reference?: string;
  @IsOptional() @IsUUID() bankAccountId?: string;
  @IsOptional() @IsString() notes?: string;
  @IsOptional() @IsArray() invoiceAllocations?: InvoiceAllocationDto[];
}

export class InvoiceAllocationDto {
  @IsUUID() invoiceId: string;
  @IsNumber() @Min(0) amount: number;
}
