import { Type } from 'class-transformer';
import { IsArray, IsDateString, IsIn, IsNotEmpty, IsNumber, IsOptional, IsString, Max, MaxLength, Min, ValidateNested } from 'class-validator';
import { GST_RATES } from '../../products/product.constants';

export class CreateQuoteItemDto {
  @IsString()
  @IsOptional()
  productId?: string;

  @IsString()
  @IsNotEmpty()
  @MaxLength(255)
  name!: string;

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

  @IsNumber()
  @Min(0.01)
  @IsOptional()
  quantity: number = 1;

  @IsNumber()
  @Min(0)
  @IsOptional()
  unitPrice: number = 0;

  @IsNumber()
  @Min(0)
  @Max(100)
  @IsOptional()
  discountPercent: number = 0;

  @IsOptional()
  @IsIn(GST_RATES, { message: 'Select a valid GST rate' })
  gstRate: number = 18.0;
}

export class CreateQuoteDto {
  @IsString()
  @IsOptional()
  @MaxLength(100)
  quoteNumber?: string;

  @IsString()
  @IsNotEmpty()
  accountId!: string;

  @IsString()
  @IsNotEmpty({ message: 'Service is required' })
  @MaxLength(120)
  serviceName!: string;

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

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

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

  @IsDateString()
  @IsOptional()
  issueDate?: string;

  @IsDateString()
  @IsNotEmpty()
  expiryDate!: string;

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

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

  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => CreateQuoteItemDto)
  @IsOptional()
  items?: CreateQuoteItemDto[] = [];
}
