import {
  IsString,
  IsNotEmpty,
  MaxLength,
  IsOptional,
  IsBoolean,
  IsArray,
  ValidateNested,
  IsIn,
  IsNumber,
} from 'class-validator';
import { Type } from 'class-transformer';

// ── Button Config DTO ────────────────────

export class ButtonConfigDto {
  @IsString()
  @IsNotEmpty()
  key: string;

  @IsString()
  @IsNotEmpty()
  label: string;

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

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

  @IsString()
  @IsIn(['toolbar', 'row', 'both'])
  position: 'toolbar' | 'row' | 'both';

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

  @IsBoolean()
  @IsOptional()
  confirm?: boolean;

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

// ── Column Config DTO ────────────────────

export class ColumnConfigDto {
  @IsString()
  @IsNotEmpty()
  key: string;

  @IsString()
  @IsNotEmpty()
  label: string;

  @IsString()
  @IsIn(['text', 'currency', 'date', 'badge', 'boolean', 'link', 'image', 'number'])
  type: 'text' | 'currency' | 'date' | 'badge' | 'boolean' | 'link' | 'image' | 'number';

  @IsBoolean()
  @IsOptional()
  sortable?: boolean;

  @IsBoolean()
  @IsOptional()
  default_visible?: boolean;

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

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

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

  @IsOptional()
  badge_colors?: Record<string, string>;
}

// ── Main Module DTO ──────────────────────

export class CreateModuleDto {
  @IsString()
  @IsNotEmpty()
  @MaxLength(150)
  name: string;

  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  slug: string;

  @IsString()
  @IsOptional()
  @MaxLength(100)
  api_prefix?: string;

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

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

  // ── NEW FIELDS ─────────────────────────

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

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

  @IsString()
  @IsOptional()
  @MaxLength(255)
  table_endpoint?: string;
}