import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsArray, IsDateString, IsIn, IsOptional, IsString, IsUUID } from 'class-validator';

import { DJIBOUTI_INCIDENT_TYPES, type DjiboutiIncidentType } from '../entities/djibouti-incident.entity';
import {
  EMPTY_CONTAINER_RETURN_STATUSES,
  type EmptyContainerReturnStatus,
} from '../entities/empty-container-return.entity';
import {
  IMPORT_CUSTOMS_RISK_LEVELS,
  type ImportCustomsDocumentType,
  type ImportCustomsRiskLevel,
} from '../entities/import-customs-finalization.entity';

export const IMPORT_CUSTOMS_DOCUMENT_TYPES = [
  'IM4',
  'IM5',
  'T1_CLOSURE_PROOF',
  'TRANSIT_PERMIT_SCREENSHOT',
  'CUSTOMER_PAYMENT_SLIP',
  'IMPORT_RELEASE_PERMIT',
] as const;

export class CreateDjiboutiIncidentDto {
  @ApiProperty({ format: 'uuid' })
  @IsUUID()
  bookingId!: string;

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

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  cargoId?: string;

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

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

  @ApiProperty({ enum: DJIBOUTI_INCIDENT_TYPES })
  @IsIn(DJIBOUTI_INCIDENT_TYPES)
  incidentType!: DjiboutiIncidentType;

  @ApiProperty()
  @IsString()
  description!: string;

  @ApiPropertyOptional({ type: [String] })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  photos?: string[];

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

  @ApiPropertyOptional()
  @IsOptional()
  @IsDateString()
  reportedAt?: string;
}

export class UploadImportCustomsDocumentDto {
  @ApiProperty({ enum: IMPORT_CUSTOMS_DOCUMENT_TYPES })
  @IsIn(IMPORT_CUSTOMS_DOCUMENT_TYPES)
  documentType!: ImportCustomsDocumentType;

  @ApiProperty()
  @IsString()
  fileId!: string;

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

export class RecordDeclarationDto {
  @ApiProperty()
  @IsString()
  declarationSerialNumber!: string;

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

export class AssignCustomsRiskDto {
  @ApiProperty({ enum: IMPORT_CUSTOMS_RISK_LEVELS })
  @IsIn(IMPORT_CUSTOMS_RISK_LEVELS)
  risk!: ImportCustomsRiskLevel;

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

export class ImportOperationActionDto {
  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  performedBy?: string;

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

export class CreateEmptyContainerReturnDto {
  @ApiProperty()
  @IsString()
  containerNumber!: string;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  bookingId?: string;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  customerId?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsDateString()
  returnDate?: string;

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

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

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

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

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

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

export class UpdateEmptyContainerReturnStatusDto extends ImportOperationActionDto {
  @ApiProperty({ enum: EMPTY_CONTAINER_RETURN_STATUSES })
  @IsIn(EMPTY_CONTAINER_RETURN_STATUSES)
  status!: EmptyContainerReturnStatus;

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

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