import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, MaxLength, IsEmail } from 'class-validator';

/**
 * CSV Rate Upload DTO
 *
 * Request DTO for uploading CSV rate files (ADMIN only)
 */
export class CsvRateUploadDto {
  @ApiProperty({
    description: 'Name of the carrier company',
    example: 'SSC Consolidation',
    maxLength: 255,
  })
  @IsNotEmpty()
  @IsString()
  @MaxLength(255)
  companyName: string;

  @ApiProperty({
    description: 'Email address of the carrier company for booking requests',
    example: 'bookings@sscconsolidation.com',
    maxLength: 255,
  })
  @IsNotEmpty()
  @IsEmail()
  @MaxLength(255)
  companyEmail: string;

  @ApiProperty({
    description: 'CSV file containing shipping rates',
    type: 'string',
    format: 'binary',
  })
  file: any; // Will be handled by multer
}

/**
 * CSV Rate Upload Response DTO
 */
export class CsvRateUploadResponseDto {
  @ApiProperty({
    description: 'Upload success status',
    example: true,
  })
  success: boolean;

  @ApiProperty({
    description: 'Number of rate rows parsed from CSV',
    example: 25,
  })
  ratesCount: number;

  @ApiProperty({
    description: 'Path where CSV file was saved',
    example: 'ssc-consolidation.csv',
  })
  csvFilePath: string;

  @ApiProperty({
    description: 'Company name for which rates were uploaded',
    example: 'SSC Consolidation',
  })
  companyName: string;

  @ApiProperty({
    description: 'Upload timestamp',
    example: '2025-10-23T10:30:00Z',
  })
  uploadedAt: Date;
}

/**
 * CSV Rate Config Response DTO
 *
 * Configuration entry for a company's CSV rates
 */
export class CsvRateConfigDto {
  @ApiProperty({
    description: 'Configuration ID',
    example: '123e4567-e89b-12d3-a456-426614174000',
  })
  id: string;

  @ApiProperty({
    description: 'Company name',
    example: 'SSC Consolidation',
  })
  companyName: string;

  @ApiProperty({
    description: 'CSV file path',
    example: 'ssc-consolidation.csv',
  })
  csvFilePath: string;

  @ApiProperty({
    description: 'Integration type',
    enum: ['CSV_ONLY', 'CSV_AND_API'],
    example: 'CSV_ONLY',
  })
  type: 'CSV_ONLY' | 'CSV_AND_API';

  @ApiProperty({
    description: 'Whether company has API connector',
    example: false,
  })
  hasApi: boolean;

  @ApiProperty({
    description: 'API connector name if hasApi is true',
    example: null,
    nullable: true,
  })
  apiConnector: string | null;

  @ApiProperty({
    description: 'Whether configuration is active',
    example: true,
  })
  isActive: boolean;

  @ApiProperty({
    description: 'When CSV was last uploaded',
    example: '2025-10-23T10:30:00Z',
  })
  uploadedAt: Date;

  @ApiProperty({
    description: 'Number of rate rows in CSV',
    example: 25,
    nullable: true,
  })
  rowCount: number | null;

  @ApiProperty({
    description: 'Additional metadata',
    example: { description: 'LCL rates for Europe to US', coverage: 'Global' },
    nullable: true,
  })
  metadata: Record<string, any> | null;
}

/**
 * CSV File Validation Result DTO
 */
export class CsvFileValidationDto {
  @ApiProperty({
    description: 'Whether CSV file is valid',
    example: true,
  })
  valid: boolean;

  @ApiProperty({
    description: 'Validation errors if any',
    type: [String],
    example: [],
  })
  errors: string[];

  @ApiProperty({
    description: 'Number of rows in CSV file',
    example: 25,
    required: false,
  })
  rowCount?: number;
}

/**
 * Available Companies Response DTO
 */
export class AvailableCompaniesDto {
  @ApiProperty({
    description: 'List of available company names',
    type: [String],
    example: ['SSC Consolidation', 'ECU Worldwide', 'TCC Logistics', 'NVO Consolidation'],
  })
  companies: string[];

  @ApiProperty({
    description: 'Total number of companies',
    example: 4,
  })
  total: number;
}

/**
 * Filter Options Response DTO
 */
export class FilterOptionsDto {
  @ApiProperty({
    description: 'Available company names',
    type: [String],
    example: ['SSC Consolidation', 'ECU Worldwide', 'TCC Logistics', 'NVO Consolidation'],
  })
  companies: string[];

  @ApiProperty({
    description: 'Available container types',
    type: [String],
    example: ['LCL', '20DRY', '40HC', '40DRY'],
  })
  containerTypes: string[];

  @ApiProperty({
    description: 'Supported currencies',
    type: [String],
    example: ['USD', 'EUR'],
  })
  currencies: string[];
}

/**
 * Port Info for Route Response DTO
 * Contains port details with coordinates for map display
 */
export class RoutePortInfoDto {
  @ApiProperty({
    description: 'UN/LOCODE port code',
    example: 'NLRTM',
  })
  code: string;

  @ApiProperty({
    description: 'Port name',
    example: 'Rotterdam',
  })
  name: string;

  @ApiProperty({
    description: 'City name',
    example: 'Rotterdam',
  })
  city: string;

  @ApiProperty({
    description: 'Country code (ISO 3166-1 alpha-2)',
    example: 'NL',
  })
  country: string;

  @ApiProperty({
    description: 'Country full name',
    example: 'Netherlands',
  })
  countryName: string;

  @ApiProperty({
    description: 'Display name for UI',
    example: 'Rotterdam, Netherlands (NLRTM)',
  })
  displayName: string;

  @ApiProperty({
    description: 'Latitude coordinate',
    example: 51.9244,
    required: false,
  })
  latitude?: number;

  @ApiProperty({
    description: 'Longitude coordinate',
    example: 4.4777,
    required: false,
  })
  longitude?: number;
}

/**
 * Available Origins Response DTO
 * Returns list of origin ports that have routes in CSV rates
 */
export class AvailableOriginsDto {
  @ApiProperty({
    description: 'List of origin ports with available routes in CSV rates',
    type: [RoutePortInfoDto],
  })
  origins: RoutePortInfoDto[];

  @ApiProperty({
    description: 'Total number of available origin ports',
    example: 15,
  })
  total: number;
}

/**
 * Available Destinations Response DTO
 * Returns list of destination ports available for a given origin
 */
export class AvailableDestinationsDto {
  @ApiProperty({
    description: 'Origin port code that was used to filter destinations',
    example: 'NLRTM',
  })
  origin: string;

  @ApiProperty({
    description: 'List of destination ports available from the given origin',
    type: [RoutePortInfoDto],
  })
  destinations: RoutePortInfoDto[];

  @ApiProperty({
    description: 'Total number of available destinations for this origin',
    example: 8,
  })
  total: number;
}
