import { typeParser } from '@lib/utils/type-parser';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Expose, Transform } from 'class-transformer';
import { IsEnum, IsIn, IsInt, IsOptional, IsString } from 'class-validator';

//* 정산상태(ast_settle_status)
export enum AgencySettleStatus {
  PENDING = 'PENDING',
  CONFIRMED = 'CONFIRMED',
}

//* 주문별 정산반영(som_settle_action)
export enum AgencySettleAction {
  INCLUDE = 'INCLUDE',
  EXCLUDE = 'EXCLUDE',
  CARRYOVER = 'CARRYOVER',
}

export class AgencySettlement {
  @Expose()
  @ApiProperty({ description: 'agency settlement id' })
  @Transform(({ value }) => typeParser.int(value))
  @IsInt()
  astId!: number;

  @Expose()
  @ApiProperty({ description: '정산코드' })
  @IsString()
  astCode!: string;

  @Expose()
  @ApiProperty({ description: '정산연월' })
  @IsString()
  astDate!: string;

  @Expose()
  @ApiProperty({ description: '총 구독주문건 수' })
  @Transform(({ value }) => typeParser.int(value))
  @IsInt()
  astOrderCount!: number;

  @Expose()
  @ApiProperty({ description: '총 분석건 수' })
  @Transform(({ value }) => typeParser.int(value))
  @IsInt()
  astOrderTotalLimitedQty!: number;

  @Expose()
  @ApiProperty({ description: '총 사용건 수' })
  @Transform(({ value }) => typeParser.int(value))
  @IsInt()
  astOrderTotalUsedQty!: number;

  @Expose()
  @ApiProperty({ description: '정산청구금액' })
  @IsInt()
  astSettlementPrice!: number;

  @Expose()
  @ApiProperty({ description: '입금완료여부(default:0, 완료:1|미완료:0)' })
  @Transform(({ value }) => typeParser.int(value))
  @IsIn([0, 1])
  astPaid!: number;

  @Expose()
  @ApiProperty({ description: '입금완료일' })
  @IsString()
  @IsOptional()
  astPaidAt?: string;

  @Expose()
  @ApiProperty({
    description: '정산상태 (PENDING: 미정산 | CONFIRMED: 정산확정)',
    enum: AgencySettleStatus,
  })
  @IsEnum(AgencySettleStatus)
  astSettleStatus!: AgencySettleStatus;

  @Expose()
  @ApiPropertyOptional({ description: '정산확정일시' })
  @IsString()
  @IsOptional()
  astConfirmedAt?: string | null;

  @Expose()
  @ApiPropertyOptional({ description: '정산확정 처리 관리자 id' })
  @Transform(({ value }) => (value == null ? null : typeParser.int(value)))
  @IsInt()
  @IsOptional()
  astConfirmedBy?: number | null;

  @Expose()
  @ApiPropertyOptional({
    description: '정산확정금액 스냅샷 (확정 시 INCLUDE 합, 미정산이면 null)',
  })
  @Transform(({ value }) => (value == null ? null : typeParser.int(value)))
  @IsInt()
  @IsOptional()
  astConfirmedPrice?: number | null;

  @Expose()
  @ApiProperty({
    description: '세금계산서 발행여부(수기, default:0, 발행:1|미발행:0)',
  })
  @Transform(({ value }) => typeParser.int(value))
  @IsIn([0, 1])
  astTaxInvoice!: number;

  @Expose()
  @ApiPropertyOptional({ description: '세금계산서 발행일(수기)' })
  @IsString()
  @IsOptional()
  astTaxInvoiceAt?: string | null;

  @Expose()
  @ApiPropertyOptional({ description: '정산생성일시' })
  @IsString()
  astCreatedAt!: string;

  @Expose()
  @ApiProperty({ description: '정산수정일시' })
  @IsString()
  @IsOptional()
  astUpdatedAt?: string;
}
