import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
  IsString,
  IsArray,
  ValidateNested,
  IsNumber,
  Min,
  IsOptional,
  ArrayMinSize,
  IsNotEmpty,
} from 'class-validator';
import type { OrderCarAddress, OrderUserAddress } from '../interface/order.interface';

export class AdminOrderItemDto {
  @ApiProperty({ description: 'Food ID' })
  @IsString()
  @IsNotEmpty()
  foodId: string;

  @ApiProperty({ description: 'Quantity', minimum: 1 })
  @IsNumber()
  @Min(1)
  quantity: number;
}

export class AdminCreateOrderDto {
  @ApiProperty({ description: 'Phone number of the user to create the order for' })
  @IsString()
  @IsNotEmpty()
  userPhone: string;

  @ApiPropertyOptional({ description: 'First name — used when creating a new user' })
  @IsOptional()
  @IsString()
  firstName?: string;

  @ApiPropertyOptional({ description: 'Last name — used when creating a new user' })
  @IsOptional()
  @IsString()
  lastName?: string;

  @ApiProperty({ type: [AdminOrderItemDto], description: 'Order items' })
  @IsArray()
  @ArrayMinSize(1)
  @ValidateNested({ each: true })
  @Type(() => AdminOrderItemDto)
  items: AdminOrderItemDto[];

  @ApiProperty({ description: 'Delivery method ID' })
  @IsString()
  @IsNotEmpty()
  deliveryMethodId: string;

  @ApiProperty({ description: 'Payment method ID' })
  @IsString()
  @IsNotEmpty()
  paymentMethodId: string;

  @ApiPropertyOptional({ description: 'Order description or notes' })
  @IsOptional()
  @IsString()
  description?: string;

  @ApiPropertyOptional({ description: 'Table number (required for dine-in)' })
  @IsOptional()
  @IsString()
  tableNumber?: string;

  @ApiPropertyOptional({
    description: 'User delivery address (required for courier delivery)',
    example: {
      fullName: 'علی محمدی',
      phone: '09121234567',
      city: 'تهران',
      province: 'تهران',
      address: 'خیابان ولیعصر، پلاک ۱۲',
      postalCode: '1234567890',
      latitude: 35.6892,
      longitude: 51.389,
    },
  })
  @IsOptional()
  userAddress?: OrderUserAddress;

  @ApiPropertyOptional({
    description: 'Car address (required for car delivery)',
    example: {
      carModel: 'پراید',
      carColor: 'سفید',
      plateNumber: '۱۲ ایران ۳۴۵',
      phone: '09121234567',
    },
  })
  @IsOptional()
  carAddress?: OrderCarAddress;
}
