import { IsOptional, IsString, IsDateString, IsArray, IsEnum, IsBoolean, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';

export enum SegmentOperator {
  AND = 'AND',
  OR = 'OR',
}

export enum ConditionOperator {
  EQUALS = 'equals',
  NOT_EQUALS = 'not_equals',
  CONTAINS = 'contains',
  NOT_CONTAINS = 'not_contains',
  IN = 'in',
  NOT_IN = 'not_in',
  GREATER_THAN = 'greater_than',
  LESS_THAN = 'less_than',
  BETWEEN = 'between',
}

export class SegmentConditionDto {
  @IsString()
  dimension!: string;

  @IsEnum(ConditionOperator)
  operator!: ConditionOperator;

  @IsString({ each: true })
  @IsArray()
  values!: string[];
}

export class CreateSegmentDto {
  @IsString()
  name!: string;

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

  @ValidateNested({ each: true })
  @Type(() => SegmentConditionDto)
  @IsArray()
  conditions!: SegmentConditionDto[];

  @IsOptional()
  @IsEnum(SegmentOperator)
  operator?: SegmentOperator = SegmentOperator.AND;

  @IsOptional()
  @IsBoolean()
  isBuiltIn?: boolean = false;
}

export class UpdateSegmentDto {
  @IsOptional()
  @IsString()
  name?: string;

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

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

  @IsOptional()
  @IsEnum(SegmentOperator)
  operator?: SegmentOperator;
}

export class ApplySegmentQueryDto {
  @IsDateString()
  startDate!: string;

  @IsDateString()
  endDate!: string;

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

export class CompareSegmentsQueryDto {
  @IsDateString()
  startDate!: string;

  @IsDateString()
  endDate!: string;

  @IsString({ each: true })
  @IsArray()
  segmentIds!: string[];

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