import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import {
  IsBoolean,
  IsDateString,
  IsIn,
  IsInt,
  IsNumber,
  IsOptional,
  IsString,
  Max,
  Min,
  MinLength,
} from 'class-validator';

export class RequestChangesDto {
  @ApiProperty({ description: 'Staff note explaining what the customer must fix' })
  @IsString()
  @MinLength(1)
  note!: string;
}

export class AcceptIntakeDto {
  @ApiProperty({
    description:
      'How many days the contract stays valid, counted from the accept date. ' +
      'The contract is valid from now through now + validityDays.',
    minimum: 1,
    maximum: 365,
    example: 30,
  })
  @IsInt()
  @Min(1)
  @Max(365)
  validityDays!: number;
}

export class StaffRejectDto {
  @ApiProperty()
  @IsString()
  @MinLength(1)
  reason!: string;
}

export class ApproveStepDto {
  @ApiProperty({ description: 'LINE_STAFF | DIRECTOR | CEO' })
  @IsString()
  requiredRole!: string;
}

export class RejectStepDto {
  @ApiProperty()
  @IsString()
  @MinLength(1)
  reason!: string;
}

export class CancelBookingDto {
  @ApiProperty()
  @IsString()
  @MinLength(1)
  reason!: string;
}

export class RejectBookingDto {
  @ApiPropertyOptional({
    description: 'Optional reason the customer rejected the price estimate',
  })
  @IsOptional()
  @IsString()
  reason?: string;
}

export class AdjustPriceDto {
  @ApiPropertyOptional({
    description:
      'New total price. Omit or send null to clear a previous adjustment.',
  })
  @IsOptional()
  @IsNumber()
  @Min(0)
  amount?: number | null;

  @ApiPropertyOptional({ description: 'Reason for the adjustment' })
  @IsOptional()
  @IsString()
  reason?: string;
}

export class ReviewDocumentDto {
  @ApiProperty({ description: 'The document fileKey being reviewed' })
  @IsString()
  @MinLength(1)
  fileKey!: string;

  @ApiProperty({ enum: ['APPROVED', 'QUERIED'] })
  @IsIn(['APPROVED', 'QUERIED'])
  status!: 'APPROVED' | 'QUERIED';

  @ApiPropertyOptional({ description: 'Required when querying a document' })
  @IsOptional()
  @IsString()
  note?: string;
}

export class RequestOperationDto {
  @ApiProperty({
    description:
      'The schedule day (train departure day) the customer selects for this ' +
      'shipment. ISO date — the booking enters the batch pool for this route + day.',
    example: '2026-07-15',
  })
  @IsDateString()
  scheduledDate!: string;
}

export class OperationReviewDto {
  @ApiProperty({
    description:
      'The operations decision: ACCEPT enters the batch pool; REQUEST_CHANGES ' +
      'returns it to the customer with a note; ADJUST_PRICE sets a new total the ' +
      'customer must re-confirm before it proceeds.',
    enum: ['ACCEPT', 'REQUEST_CHANGES', 'ADJUST_PRICE'],
  })
  @IsIn(['ACCEPT', 'REQUEST_CHANGES', 'ADJUST_PRICE'])
  decision!: 'ACCEPT' | 'REQUEST_CHANGES' | 'ADJUST_PRICE';

  @ApiPropertyOptional({
    description: 'Required for REQUEST_CHANGES (what the customer must fix).',
  })
  @IsOptional()
  @IsString()
  note?: string;

  @ApiPropertyOptional({
    description: 'New total price — required for ADJUST_PRICE.',
  })
  @IsOptional()
  @IsNumber()
  @Min(0)
  amount?: number;
}

export class ConfirmOperationPriceDto {
  @ApiProperty({
    description:
      'true to accept the operations price adjustment and proceed to the ' +
      'batch pool; false to reject it (returns to operation changes requested).',
  })
  @IsBoolean()
  accept!: boolean;
}
