import { Field, Int, ObjectType } from '@nestjs/graphql';
import {
  Anomaly,
  AttendanceRegistration,
  Employee,
  PlannedTime,
  SolutionTarget,
  SolutionTargetStep,
  WorkTime,
} from '@prisma/client';
import { JsonValue } from '@prisma/client/runtime/library';
import { Paginated } from 'src/core/base/pagination.input';
import { AnomalyAsssociatedEntities } from '../feat-business-objects/entities/anomaly/anomalies.dto';
import { TourObjectType } from '../app-accounting/tour.object-type';

@ObjectType('PlannedTime')
export class PlannedTimeObjectType implements PlannedTime {
  @Field()
  id: string;
  @Field()
  userId: string;
  @Field()
  firstname: string;
  @Field()
  surname: string;
  @Field(() => Date)
  startDate: Date;
  @Field(() => Date)
  endDate: Date;
  @Field()
  department: string;
  @Field(() => Int)
  pause: number;
  @Field(() => Int)
  duration: number;
  @Field({nullable: true})
  note: string;
}

@ObjectType('WorkTime')
export class WorkTimeObjectType implements WorkTime {
  @Field()
  id: string;
  @Field()
  userId: string;
  @Field()
  firstname: string;
  @Field()
  surname: string;
  @Field(() => Date)
  startDate: Date;
  @Field(() => Date)
  endDate: Date;
  @Field()
  department: string;
  @Field(() => Int)
  pause: number;
  @Field(() => Int)
  duration: number;
}

@ObjectType('Employee')
export class EmployeeObjectType implements Employee {
  @Field()
  name: string;
  @Field()
  id: string;
  @Field()
  surname: string;
  @Field()
  personnelNumber: string;
  // @Field()
  mobile: string;
  @Field({ nullable: true })
  qualification: string;
  @Field(() => Date, { nullable: true })
  occuptationalHealthExamination: Date;
  @Field(() => Date, { nullable: true })
  personTransportCertificate: Date;
}

@ObjectType('AttendanceRegistration')
export class AttendanceRegistrationObjectType
  implements AttendanceRegistration
{
  @Field()
  personnelNumber: string;
  @Field(() => Int)
  attendanceRegistrationId: number;
  @Field(() => Int)
  employeeId: number;
  @Field(() => Date)
  dateTime: Date;
  @Field()
  event: string;
}

@ObjectType('AssociatedEntities')
export class AssociatedEntitiesObjectType
  implements AnomalyAsssociatedEntities
{
  @Field(() => [TourObjectType], { nullable: true })
  tour?: TourObjectType[];
  @Field(() => [EmployeeObjectType], { nullable: true })
  employee?: EmployeeObjectType[];
  @Field(() => [PlannedTimeObjectType], { nullable: true })
  plannedTime?: PlannedTimeObjectType[];
  @Field(() => [WorkTimeObjectType], { nullable: true })
  workTime?: WorkTimeObjectType[];
  @Field(() => [AttendanceRegistrationObjectType], { nullable: true })
  attendanceRegistration?: AttendanceRegistrationObjectType[];
}

@ObjectType('SolutionTarget')
export class SolutionTargetObjectType implements SolutionTarget {
  @Field()
  id: string;
  @Field()
  name: string;
  @Field()
  anomalyId: string;
}

@ObjectType('SolutionTargetStep')
export class SolutionTargetStepObjectType implements SolutionTargetStep {
  @Field()
  id: string;
  @Field()
  description: string;
  @Field()
  solutionTargetId: string;
}

@ObjectType('Anomaly')
export class AnomalyObjectType implements Anomaly {
  @Field()
  id: string;
  @Field()
  type: string;
  @Field()
  groupKey: string;
  @Field()
  groupDescription: string;
  // @Field()
  associatedEntities: JsonValue;
  @Field()
  workedOnBy: string;
  @Field(() => Int)
  sleepTimer: number;
  @Field({ nullable: true })
  sleepSince: Date | null;
  @Field()
  description: string;
  @Field({ nullable: true })
  resolvedAt: Date | null;
  @Field({ nullable: true })
  manuallyResolved: boolean | null;
  @Field()
  createdAt: Date;
}

@ObjectType('PaginatedAnomaly')
export class PaginatedAnomalyObjectType extends Paginated(AnomalyObjectType) {}

@ObjectType('AnomalyGroup')
export class AnomalyGroupObjectType {
  @Field()
  key: string;
  @Field(() => [AnomalyObjectType])
  anomalies: AnomalyObjectType[];
}

@ObjectType('PaginatedAnomalyGroup')
export class PaginatedAnomalyGroupObjectType extends Paginated(
  AnomalyGroupObjectType,
) {}
