import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { TransferCategory, TransferCondition, TransferPricingSource } from '@prisma/client';
import { Type } from 'class-transformer';
import { IsArray, IsBoolean, IsEnum, IsInt, IsNotEmpty, IsNumber, IsOptional, IsString, ValidateNested } from 'class-validator';

export class CreateTransferItemDto {
  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  name!: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  brand?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  modelName?: string;

  @ApiProperty({ enum: TransferCategory })
  @IsEnum(TransferCategory)
  category!: TransferCategory;

  @ApiProperty({ enum: TransferCondition })
  @IsEnum(TransferCondition)
  condition!: TransferCondition;

  @ApiPropertyOptional()
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  purchaseYear?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @Type(() => Number)
  originalPriceVND?: number;

  @ApiProperty()
  @IsNumber()
  @Type(() => Number)
  askingPriceVND!: number;

  @ApiPropertyOptional({ default: 1 })
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  quantity?: number;

  @ApiPropertyOptional()
  @IsOptional()
  dimensions?: Record<string, unknown>;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  notes?: string;
}

export class CreateTransferListingDto {
  @ApiProperty({ enum: TransferCategory })
  @IsEnum(TransferCategory)
  category!: TransferCategory;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  title!: string;

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

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  address!: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  ward?: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  district!: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  city!: string;

  @ApiProperty()
  @IsNumber()
  latitude!: number;

  @ApiProperty()
  @IsNumber()
  longitude!: number;

  @ApiProperty()
  @IsNumber()
  @Type(() => Number)
  askingPriceVND!: number;

  @ApiPropertyOptional({ enum: TransferPricingSource, default: 'MANUAL' })
  @IsOptional()
  @IsEnum(TransferPricingSource)
  pricingSource?: TransferPricingSource;

  @ApiPropertyOptional({ default: true })
  @IsOptional()
  @IsBoolean()
  isNegotiable?: boolean;

  @ApiPropertyOptional()
  @IsOptional()
  @IsNumber()
  @Type(() => Number)
  areaM2?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @Type(() => Number)
  monthlyRentVND?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  depositMonths?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  remainingLeaseMo?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  businessType?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  footTraffic?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  contactPhone?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  contactName?: string;

  @ApiPropertyOptional({ type: [CreateTransferItemDto] })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => CreateTransferItemDto)
  items?: CreateTransferItemDto[];
}
