import { ApiHideProperty, ApiProperty } from '@nestjs/swagger';
import { plainToInstance, Transform, Type } from 'class-transformer';
import {
  IsArray,
  IsIn,
  IsInt,
  IsNumberString,
  IsOptional,
  IsString,
  ValidateNested,
  ArrayNotEmpty,
  Max,
  Min,
} from 'class-validator';

const stringList = ({ value }: { value: unknown }): string[] | undefined => {
  if (value === undefined || value === null || value === '') return undefined;
  const values = Array.isArray(value) ? value : [value];
  return values
    .flatMap((item) => String(item).split(','))
    .map((item) => item.trim())
    .filter(Boolean);
};

const tagFilters = ({ value }: { value: unknown }): unknown => {
  if (value === undefined || value === null || value === '') return undefined;
  const values = Array.isArray(value) ? value : [value];
  try {
    return values.flatMap((item) => {
      if (typeof item !== 'string') return [item];
      const parsed = JSON.parse(item) as unknown;
      return Array.isArray(parsed) ? parsed : [parsed];
    }).map((item) => plainToInstance(PublicTagFilterDto, item));
  } catch {
    return value;
  }
};

export class PublicTagFilterDto {
  @ApiProperty({ example: '1' })
  @IsNumberString()
  tagGroupId!: string;

  @ApiProperty({ type: [String], example: ['11', '12', '13'] })
  @IsArray()
  @ArrayNotEmpty()
  @IsNumberString({}, { each: true })
  tagIds!: string[];
}

export class PublicQueryGoodDto {
  @ApiProperty({ required: false, default: 1 })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  page: number = 1;

  @ApiProperty({ required: false, default: 20, maximum: 200 })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  @Max(200)
  pageSize: number = 20;

  @ApiProperty({ required: false, type: String, description: '不传表示全部国家' })
  @IsOptional()
  @IsNumberString()
  countryId?: string;

  @ApiProperty({ required: false, type: String })
  @IsOptional()
  @IsNumberString()
  categoryId?: string;

  @ApiHideProperty()
  @IsOptional()
  @Transform(tagFilters)
  @IsArray()
  @ValidateNested({ each: true })
  tags?: PublicTagFilterDto[];

  @ApiProperty({ required: false })
  @IsOptional()
  @IsString()
  minPrice?: string;

  @ApiProperty({ required: false })
  @IsOptional()
  @IsString()
  maxPrice?: string;

  @ApiProperty({ required: false, enum: ['DEFAULT', 'PRICE_ASC', 'PRICE_DESC', 'NEWEST'] })
  @IsOptional()
  @IsIn(['DEFAULT', 'PRICE_ASC', 'PRICE_DESC', 'NEWEST'])
  sort?: 'DEFAULT' | 'PRICE_ASC' | 'PRICE_DESC' | 'NEWEST';

  @ApiProperty({ required: false })
  @IsOptional()
  @IsString()
  keyword?: string;
}

export class PublicCountryQueryDto {
  @ApiProperty({ required: false, type: String, description: '不传表示全部国家' })
  @IsOptional()
  @IsNumberString()
  countryId?: string;
}

export class PublicHomeGoodsQueryDto extends PublicCountryQueryDto {
  @ApiProperty({ required: false, default: 10, maximum: 50 })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  @Max(50)
  limit: number = 10;
}
