import { GpsFixType, UtilityType } from '@prisma/client';
import { Type } from 'class-transformer';
import {
  ArrayMaxSize,
  ArrayMinSize,
  IsArray,
  IsDateString,
  IsEnum,
  IsInt,
  IsNotEmpty,
  IsNumber,
  IsOptional,
  IsString,
  Max,
  MaxLength,
  Min,
  ValidateNested,
} from 'class-validator';

export class MqttPointDto {
  @IsNumber()
  @Min(-90)
  @Max(90)
  lat: number;

  @IsNumber()
  @Min(-180)
  @Max(180)
  lng: number;

  @IsOptional()
  @IsNumber()
  alt?: number;

  @IsOptional()
  @IsEnum(GpsFixType)
  fix?: GpsFixType;

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

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

  @IsOptional()
  @IsEnum(UtilityType)
  utility?: UtilityType;

  @IsOptional()
  @IsInt()
  seq?: number;

  @IsDateString()
  ts: string;
}

export class MqttPointsMessageDto {
  // Job is resolved by jobId when present, else by (device org, ticket)
  @IsOptional()
  @IsString()
  jobId?: string;

  @IsOptional()
  @IsString()
  @MaxLength(64)
  ticket?: string;

  @IsArray()
  @ArrayMinSize(1)
  @ArrayMaxSize(500)
  @ValidateNested({ each: true })
  @Type(() => MqttPointDto)
  points: MqttPointDto[];
}

export class MqttJobMessageDto {
  @IsString()
  @IsNotEmpty()
  @MaxLength(64)
  ticket: string;

  @IsOptional()
  @IsString()
  @MaxLength(200)
  title?: string;

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

  @IsOptional()
  @IsString()
  @MaxLength(400)
  address?: string;
}
