import {
  IsNotEmpty,
  IsOptional,
  IsString,
  IsBoolean,
  IsUUID,
  IsIn,
  IsNumber,
  MaxLength,
  Min,
  ValidateIf,
} from 'class-validator';

export class CreateTransportServiceDto {
  @IsUUID()
  @IsNotEmpty()
  destination_id: string;

  // short_code is auto-derived by the service from from_city +
  // to_city + category and is tenant-unique. Optional here for
  // seeder/import back-compat; UI never sends it.
  @IsString()
  @IsOptional()
  @MaxLength(50)
  short_code?: string;

  @IsString()
  @IsNotEmpty()
  @IsIn(['PICKUP', 'DROPOFF', 'ONE_WAY', 'TWO_WAY', 'DISPOSAL', 'HALF_DAY', 'FULL_DAY'])
  category: string;

  /**
   * Disposal services only — the at-disposal package shape. NULL for
   * point-to-point services. Required when category === 'DISPOSAL'
   * (enforced at the service layer so legacy seeders that omit it
   * still pass validation for non-disposal rows).
   */
  @IsString()
  @IsOptional()
  @IsIn(['HALF_DAY_4H', 'FULL_DAY_8H', 'FULL_DAY_10H', 'FULL_DAY_12H', 'HOURLY'])
  duty_type?: string;

  /**
   * Window length in hours — mirrors the duty_type fixed values
   * (4/8/10/12) or carries the ops-typed custom value for HOURLY.
   * Required when duty_type is set.
   */
  @IsNumber()
  @IsOptional()
  @Min(0.5)
  duration_hours?: number;

  // from_city / to_city are required for point-to-point services
  // (PICKUP / DROPOFF / ONE_WAY / TWO_WAY) but optional for DISPOSAL
  // — disposal rows don't carry a real route. ValidateIf skips
  // IsNotEmpty when category=DISPOSAL so empty strings pass through
  // to the service layer, which persists them as null/descriptor.
  @ValidateIf((o) => o.category !== 'DISPOSAL')
  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  from_city: string;

  @IsString()
  @IsOptional()
  @MaxLength(500)
  from_address?: string;

  @IsString()
  @IsOptional()
  @MaxLength(20)
  from_gps_latitude?: string;

  @IsString()
  @IsOptional()
  @MaxLength(20)
  from_gps_longitude?: string;

  @ValidateIf((o) => o.category !== 'DISPOSAL')
  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  to_city: string;

  @IsString()
  @IsOptional()
  @MaxLength(500)
  to_address?: string;

  @IsString()
  @IsOptional()
  @MaxLength(20)
  to_gps_latitude?: string;

  @IsString()
  @IsOptional()
  @MaxLength(20)
  to_gps_longitude?: string;

  @IsString()
  @IsOptional()
  @MaxLength(100)
  return_from_city?: string;

  @IsString()
  @IsOptional()
  @MaxLength(500)
  return_from_address?: string;

  @IsString()
  @IsOptional()
  @MaxLength(20)
  return_from_gps_latitude?: string;

  @IsString()
  @IsOptional()
  @MaxLength(20)
  return_from_gps_longitude?: string;

  @IsString()
  @IsOptional()
  @MaxLength(100)
  return_to_city?: string;

  @IsString()
  @IsOptional()
  @MaxLength(500)
  return_to_address?: string;

  @IsString()
  @IsOptional()
  @MaxLength(20)
  return_to_gps_latitude?: string;

  @IsString()
  @IsOptional()
  @MaxLength(20)
  return_to_gps_longitude?: string;

  @IsNumber()
  @IsOptional()
  @Min(0)
  distance_km?: number;

  @IsNumber()
  @IsOptional()
  @Min(0)
  duration_mins?: number;

  /**
   * Return-leg duration — only relevant for TWO_WAY services.
   * Null/omitted means "mirror the outbound duration". Frontend
   * hides the field for non-TWO_WAY categories.
   */
  @IsNumber()
  @IsOptional()
  @Min(0)
  return_duration_mins?: number;

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

  /**
   * Flag this service as an airport transfer. Booking forms expose
   * flight-detail fields (flight number, terminal, departure/arrival
   * time) on transport items that reference this service, and the
   * Movement Planner surfaces those details in the row's hover tip.
   */
  @IsBoolean()
  @IsOptional()
  is_airport_service?: boolean;

  @IsBoolean()
  @IsOptional()
  is_active?: boolean;

  /**
   * WhatsApp body content injected via the wrapper's
   * {{service_body}} token when ops clicks Copy WA on a MD row
   * carrying this transport service. Two audiences → two fields.
   * See migration 1783100000000 for the columns.
   */
  @IsString()
  @IsOptional()
  customer_whatsapp_body?: string;

  @IsString()
  @IsOptional()
  supplier_whatsapp_body?: string;
}
