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

@Entity({ name: 'maintenance_costs', schema: 'freight' })
@Index(['vehicleId', 'incurredDate'])
export class MaintenanceCost extends BaseEntity {
  @Column({ name: 'vehicle_id', type: 'uuid' })
  vehicleId!: string;

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

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

  @ManyToOne(() => MaintenanceSchedule, { eager: false, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'maintenance_schedule_id' })
  maintenanceSchedule?: MaintenanceSchedule;

  @Column({ name: 'incurred_date', type: 'timestamptz' })
  incurredDate!: Date;

  @Column({ name: 'cost_amount', type: 'numeric', precision: 14, scale: 2 })
  costAmount!: number;

  @Column({ name: 'cost_type' })
  costType!: string; // 'PARTS', 'LABOR', 'DIAGNOSTICS', 'OTHER'

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

  @Column({ name: 'service_provider', nullable: true })
  serviceProvider?: string;

  @Column({ name: 'invoice_number', nullable: true })
  invoiceNumber?: string;

  @Column({ type: 'text', nullable: true })
  notes?: string;
}
