import { Field, Int, ObjectType, registerEnumType } from '@nestjs/graphql';
import {
  ApprovalState,
  Prisma,
  Ticket,
  TicketValidationState,
} from '@prisma/client';
import { Paginated } from 'src/core/base/pagination.input';

@ObjectType('ApprovalInfo')
export class ApprovalInfoObjectType {
  @Field({ nullable: true })
  isNeeded?: boolean;
  @Field({ nullable: true })
  whoRequested?: string;
  @Field({ nullable: true })
  answeredFrom?: string;
  @Field({ nullable: true })
  location?: string;
}

@ObjectType('DocumentInfo')
export class DocumentInfoObjectType {
  @Field(() => ApprovalInfoObjectType)
  approval: ApprovalInfoObjectType;
  @Field({ nullable: true })
  tdLocation: string;
}

@ObjectType('Ticket')
export class TicketObjectType implements Ticket {
  @Field()
  id: string;
  @Field(() => TicketValidationState)
  currentState: TicketValidationState;
  @Field(() => ApprovalState, { nullable: true })
  approvalState: ApprovalState | null;
  @Field(() => [String])
  errors: string[];
  @Field(() => [String])
  notes: string[];
  @Field(() => Boolean)
  isUrgent: boolean;
  @Field()
  urgency: Date;
  @Field(() => DocumentInfoObjectType, { nullable: true })
  documentInfo: Prisma.JsonValue;
  @Field()
  createdAt: Date;
}

@ObjectType('PaginatedTicket')
export class PaginatedTicketObjectType extends Paginated(TicketObjectType) {}

@ObjectType('TicketsUpdated')
export class TicketsUpdatedObjectType {
  @Field(() => Int)
  count: number;
}

@ObjectType('TicketStateMeta')
export class TicketStateMetaObjectType {
  @Field(() => Int)
  count: number;
  @Field(() => Int, { nullable: true })
  approvalRequiredCount?: number;
  @Field(() => Int, { nullable: true })
  approvalUnknownCount?: number;
  @Field(() => Int, { nullable: true })
  tdNotCompanyCount?: number;
  @Field(() => Int, { nullable: true })
  approvalNotCompanyCount?: number;
}

@ObjectType('TicketStateKeyFigures')
export class TicketStateKeyFiguresObjectType {
  @Field(() => Int)
  dwellTime: number;
}

registerEnumType(TicketValidationState, {
  name: 'TicketValidationState',
});

registerEnumType(ApprovalState, {
  name: 'ApprovalState',
});
