import { Type } from 'class-transformer';
import {
  IsArray,
  IsBoolean,
  IsNumber,
  IsObject,
  IsOptional,
  IsString,
  IsUUID,
  MaxLength,
  Min,
  ValidateNested,
} from 'class-validator';

/**
 * Used by the TaxType structure endpoints — upsert a component
 * (named sub-tax) under a tax type. Rates are NOT captured here;
 * they live on the destination's slab.
 */
export class TaxComponentDto {
  @IsUUID()
  @IsOptional()
  id?: string;

  @IsString()
  @MaxLength(100)
  name: string;

  @IsNumber()
  @IsOptional()
  @Min(0)
  sort_order?: number;
}

export class UpsertTaxTypeComponentsDto {
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => TaxComponentDto)
  components: TaxComponentDto[];
}

/**
 * Destination-side slab save — picks a tax_type and fills in the
 * rate for each component the type defines. `component_rates` is a
 * dict keyed by tax_component.id.
 */
export class UpsertTaxSlabDto {
  @IsUUID()
  tax_type_id: string;

  @IsUUID()
  destination_id: string;

  @IsObject()
  @IsOptional()
  component_rates?: Record<string, number>;

  /**
   * Fallback total rate for tax types without component structure.
   * When component_rates is supplied, service recomputes this from
   * the rate sum so the two can't diverge.
   */
  @IsNumber()
  @IsOptional()
  @Min(0)
  total_rate?: number;

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

  @IsBoolean()
  @IsOptional()
  is_active?: boolean;
}
