import { IsString, IsOptional, IsNumber, IsUUID, IsArray, ValidateNested, IsEnum } from 'class-validator';
import { Type } from 'class-transformer';
import { MatchingStatus } from '../entities/purchase-order-matching.entity';
import { MatchingLineStatus } from '../entities/purchase-matching-line.entity';
import { ExceptionType, ExceptionStatus } from '../entities/matching-exception.entity';

/**
 * DTO for matching line status information
 */
export class MatchingLineStatusDto {
  @IsUUID()
  id: string;

  @IsUUID()
  orderItemId: string;

  @IsString()
  productName: string;

  @IsNumber()
  qtyOrdered: number;

  @IsNumber()
  qtyReceived: number;

  @IsNumber()
  qtyInvoiced: number;

  @IsNumber()
  priceOrdered: number;

  @IsNumber()
  priceInvoiced: number;

  @IsNumber()
  qtyVariance: number;

  @IsNumber()
  invoiceQtyVariance: number;

  @IsNumber()
  priceVariance: number;

  @IsString()
  status: MatchingLineStatus;
}

/**
 * DTO for exception information
 */
export class MatchingExceptionDto {
  @IsUUID()
  id: string;

  @IsEnum(['over_receipt', 'short_receipt', 'over_invoice', 'short_invoice', 'price_variance'])
  exceptionType: ExceptionType;

  @IsOptional()
  @IsNumber()
  expectedValue?: number;

  @IsOptional()
  @IsNumber()
  actualValue?: number;

  @IsOptional()
  @IsNumber()
  varianceValue?: number;

  @IsOptional()
  @IsNumber()
  variancePercent?: number;

  @IsEnum(['pending', 'approved', 'rejected'])
  status: ExceptionStatus;

  @IsOptional()
  @IsString()
  resolvedAt?: string;

  @IsOptional()
  @IsString()
  resolutionNotes?: string;
}

/**
 * DTO for complete matching status response
 */
export class MatchingStatusDto {
  @IsUUID()
  id: string;

  @IsUUID()
  purchaseOrderId: string;

  @IsString()
  orderNumber: string;

  @IsEnum(['pending', 'partial_receipt', 'received', 'partial_invoice', 'matched', 'mismatch'])
  status: MatchingStatus;

  @IsNumber()
  totalOrdered: number;

  @IsNumber()
  totalReceived: number;

  @IsNumber()
  totalInvoiced: number;

  @IsNumber()
  receiptVariance: number;

  @IsNumber()
  invoiceVariance: number;

  @IsOptional()
  @IsUUID()
  lastReceiptId?: string;

  @IsOptional()
  @IsUUID()
  lastInvoiceId?: string;

  @IsOptional()
  @IsString()
  matchedAt?: string;

  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => MatchingLineStatusDto)
  lines: MatchingLineStatusDto[];

  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => MatchingExceptionDto)
  exceptions: MatchingExceptionDto[];

  @IsNumber()
  pendingExceptionsCount: number;
}

/**
 * DTO for matching summary (list view)
 */
export class MatchingSummaryDto {
  @IsUUID()
  id: string;

  @IsUUID()
  purchaseOrderId: string;

  @IsString()
  orderNumber: string;

  @IsString()
  supplierName: string;

  @IsEnum(['pending', 'partial_receipt', 'received', 'partial_invoice', 'matched', 'mismatch'])
  status: MatchingStatus;

  @IsNumber()
  totalOrdered: number;

  @IsNumber()
  totalReceived: number;

  @IsNumber()
  totalInvoiced: number;

  @IsNumber()
  pendingExceptionsCount: number;

  @IsString()
  createdAt: string;

  @IsOptional()
  @IsString()
  matchedAt?: string;
}
