import { ApiPropertyOptional } from '@nestjs/swagger';
import { Direction, Furnishing, PropertyCondition } from '@prisma/client';
import { Transform, Type } from 'class-transformer';
import {
  IsString,
  IsOptional,
  MinLength,
  MaxLength,
  IsArray,
  ValidateNested,
  IsInt,
  Min,
  IsEnum,
  IsBoolean,
  IsNumber,
  IsUUID,
  ValidateIf,
} from 'class-validator';

export class MediaOrderItemDto {
  @IsString()
  mediaId!: string;

  @IsInt()
  @Min(0)
  order!: number;
}

export class UpdateListingDto {
  @ApiPropertyOptional({ example: 'Căn hộ 3PN view sông - Vinhomes Central Park', description: 'Listing title (min 5 chars)' })
  @IsOptional()
  @IsString()
  @MinLength(5)
  title?: string;

  @ApiPropertyOptional({ example: 'Căn hộ cao cấp 3 phòng ngủ, nội thất đầy đủ...', description: 'Detailed description (min 10 chars)' })
  @IsOptional()
  @IsString()
  @MinLength(10)
  description?: string;

  @ApiPropertyOptional({ type: String, example: '5500000000', description: 'Price in VND (as string to support bigint)' })
  @IsOptional()
  @Transform(({ value }) => (value != null ? BigInt(value) : undefined))
  priceVND?: bigint;

  @ApiPropertyOptional({ type: String, example: '25000000', description: 'Monthly rent price in VND (as string to support bigint)' })
  @IsOptional()
  @Transform(({ value }) => (value != null ? BigInt(value) : undefined))
  rentPriceMonthly?: bigint;

  @ApiPropertyOptional({ example: ['Hồ bơi', 'Gym', 'Sân chơi trẻ em'], description: 'List of amenities' })
  @IsOptional()
  @IsArray()
  amenities?: string[];

  @ApiPropertyOptional({
    example: [{ mediaId: 'media-1', order: 0 }, { mediaId: 'media-2', order: 1 }],
    description: 'Reorder media items by specifying mediaId and new order',
  })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => MediaOrderItemDto)
  mediaOrder?: MediaOrderItemDto[];

  // ─── Rich property fields ──────────────────────────────
  @ApiPropertyOptional({ enum: Furnishing })
  @IsOptional()
  @IsEnum(Furnishing)
  furnishing?: Furnishing;

  @ApiPropertyOptional({ enum: PropertyCondition })
  @IsOptional()
  @IsEnum(PropertyCondition)
  propertyCondition?: PropertyCondition;

  @ApiPropertyOptional({ enum: Direction })
  @IsOptional()
  @IsEnum(Direction)
  balconyDirection?: Direction;

  @ApiPropertyOptional({ type: String, example: '2500000' })
  @IsOptional()
  @Transform(({ value }) => (value != null && value !== '' ? BigInt(value) : undefined))
  maintenanceFeeVND?: bigint;

  @ApiPropertyOptional({ example: 2 })
  @IsOptional()
  @IsNumber()
  @Type(() => Number)
  @Min(0)
  parkingSlots?: number;

  @ApiPropertyOptional({ example: ['Sông'] })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  viewType?: string[];

  @ApiPropertyOptional({ example: true })
  @IsOptional()
  @IsBoolean()
  petFriendly?: boolean;

  @ApiPropertyOptional({ example: ['Gia đình có con nhỏ'] })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  suitableFor?: string[];

  @ApiPropertyOptional({ example: 'Vì sao khu vực này phù hợp...' })
  @IsOptional()
  @IsString()
  @MaxLength(2000)
  whyThisLocation?: string;

  @ApiPropertyOptional({
    type: String,
    nullable: true,
    description:
      'Chuyển ownership sang agent khác (UUID) hoặc `null` để bỏ gán. Chỉ admin hoặc môi giới hiện tại mới được dùng field này.',
  })
  @IsOptional()
  @ValidateIf((_, value) => value !== null)
  @IsUUID()
  agentId?: string | null;

  // propertyType, address, location CANNOT be changed after ACTIVE status.
}
