import { IsString, IsInt, IsOptional, IsArray, ValidateNested, IsBoolean, IsDateString, Min } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';

export class RouteStopInputDto {
  @ApiProperty({ example: 'station-uuid', description: 'Station UUID' }) @IsString() stationId: string;
  @ApiProperty({ example: 1, description: 'Stop order (1 = origin, ascending)' }) @IsInt() @Min(1) sequence: number;
  @ApiPropertyOptional({ example: 120, description: 'Distance in km from previous stop' }) @IsOptional() @IsInt() distanceKm?: number;
}

export class CreateRouteDto {
  @ApiProperty({ example: 'ADD-DJI', description: 'Unique route code' }) @IsString() code: string;
  @ApiProperty({ example: 'Addis Ababa – Djibouti' }) @IsString() name: string;
  @ApiPropertyOptional({ example: 'Main corridor via Dire Dawa' }) @IsOptional() @IsString() description?: string;
  @ApiProperty({ example: '2026-01-01T00:00:00Z', description: 'Date from which this route is effective' }) @IsDateString() effectiveFrom: string;
  @ApiPropertyOptional({ example: '2027-12-31T23:59:59Z' }) @IsOptional() @IsDateString() effectiveUntil?: string;
  @ApiProperty({
    type: [RouteStopInputDto],
    description: 'Ordered stops for this route. Sequence 1 = origin, last sequence = destination.',
    example: [
      { stationId: 'uuid-ADD', sequence: 1 },
      { stationId: 'uuid-ADM', sequence: 2, distanceKm: 99 },
      { stationId: 'uuid-AWS', sequence: 3, distanceKm: 120 },
      { stationId: 'uuid-DDW', sequence: 4, distanceKm: 180 },
      { stationId: 'uuid-AYS', sequence: 5, distanceKm: 95 },
      { stationId: 'uuid-DJI', sequence: 6, distanceKm: 60 },
    ],
  })
  @IsArray() @ValidateNested({ each: true }) @Type(() => RouteStopInputDto)
  stops: RouteStopInputDto[];
}

export class AddRouteStopDto {
  @ApiProperty({ example: 'station-uuid' }) @IsString() stationId: string;
  @ApiProperty({ example: 3 }) @IsInt() @Min(1) sequence: number;
  @ApiPropertyOptional({ example: 75 }) @IsOptional() @IsInt() distanceKm?: number;
}

export class UpdateRouteDto {
  @ApiPropertyOptional({ example: 'Addis Ababa – Djibouti Express' }) @IsOptional() @IsString() name?: string;
  @ApiPropertyOptional() @IsOptional() @IsString() description?: string;
  @ApiPropertyOptional({ example: true }) @IsOptional() @IsBoolean() active?: boolean;
  @ApiPropertyOptional({ example: '2027-12-31T23:59:59Z' }) @IsOptional() @IsDateString() effectiveUntil?: string;
}
