import { BaseEntity } from '@edr/api-common';
import { Entity, Column, ManyToOne, JoinColumn, Index } from 'typeorm';
import { Vehicle } from '../../vehicles/entities/vehicle.entity';
import { Driver } from '../../drivers/entities/driver.entity';

export enum IncidentType {
  ACCIDENT = 'ACCIDENT',
  BREAKDOWN = 'BREAKDOWN',
  TRAFFIC_VIOLATION = 'TRAFFIC_VIOLATION',
  THEFT = 'THEFT',
  OTHER = 'OTHER',
}

export enum IncidentSeverity {
  MINOR = 'MINOR',
  MODERATE = 'MODERATE',
  MAJOR = 'MAJOR',
  CRITICAL = 'CRITICAL',
}

export enum IncidentStatus {
  REPORTED = 'REPORTED',
  UNDER_REVIEW = 'UNDER_REVIEW',
  CLAIM_FILED = 'CLAIM_FILED',
  RESOLVED = 'RESOLVED',
  CLOSED = 'CLOSED',
}

@Entity({ name: 'incidents', schema: 'freight' })
@Index(['driverId', 'occurredAt'])
@Index(['vehicleId', 'occurredAt'])
export class Incident extends BaseEntity {
  @Column({ name: 'vehicle_id', type: 'uuid', nullable: true })
  vehicleId?: string;

  @ManyToOne(() => Vehicle, { eager: false, nullable: true })
  @JoinColumn({ name: 'vehicle_id' })
  vehicle?: Vehicle;

  @Column({ name: 'driver_id', type: 'uuid', nullable: true })
  driverId?: string;

  @ManyToOne(() => Driver, { eager: false, nullable: true })
  @JoinColumn({ name: 'driver_id' })
  driver?: Driver;

  @Column({ name: 'booking_id', type: 'uuid', nullable: true })
  bookingId?: string;

  @Column({ name: 'type', type: 'varchar' })
  type!: IncidentType;

  @Column({ name: 'severity', type: 'varchar' })
  severity!: IncidentSeverity;

  @Column({ name: 'occurred_at', type: 'timestamptz' })
  occurredAt!: Date;

  @Column({ name: 'location', type: 'varchar', nullable: true })
  location?: string;

  @Column({ name: 'description', type: 'text' })
  description!: string;

  @Column({ name: 'damage_estimate', type: 'numeric', precision: 14, scale: 2, nullable: true })
  damageEstimate?: number;

  @Column({ name: 'status', type: 'varchar', default: IncidentStatus.REPORTED })
  status!: IncidentStatus;

  @Column({ name: 'insurance_claim_number', type: 'varchar', nullable: true })
  insuranceClaimNumber?: string;

  @Column({ name: 'reported_by', type: 'varchar', nullable: true })
  reportedBy?: string;
}
