/**
 * Diagnostic Entity
 * Mecánicas Diesel - ERP Suite
 *
 * Represents diagnostic tests performed on vehicles.
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
} from 'typeorm';
import { ServiceOrder } from './service-order.entity';

export enum DiagnosticType {
  SCANNER = 'scanner',
  INJECTOR_TEST = 'injector_test',
  PUMP_TEST = 'pump_test',
  COMPRESSION = 'compression',
  TURBO_TEST = 'turbo_test',
  OTHER = 'other',
}

export enum DiagnosticResult {
  PASS = 'pass',
  FAIL = 'fail',
  NEEDS_ATTENTION = 'needs_attention',
}

@Entity({ name: 'diagnostics', schema: 'service_management' })
@Index('idx_diagnostics_tenant', ['tenantId'])
@Index('idx_diagnostics_vehicle', ['vehicleId'])
@Index('idx_diagnostics_order', ['orderId'])
export class Diagnostic {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'tenant_id', type: 'uuid' })
  tenantId: string;

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

  @Column({ name: 'vehicle_id', type: 'uuid' })
  vehicleId: string;

  @Column({ name: 'diagnostic_type', type: 'varchar', length: 50 })
  diagnosticType: DiagnosticType;

  @Column({ type: 'varchar', length: 200, nullable: true })
  equipment?: string;

  @Column({ name: 'performed_at', type: 'timestamptz', default: () => 'NOW()' })
  performedAt: Date;

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

  @Column({ type: 'varchar', length: 20, nullable: true })
  result?: DiagnosticResult;

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

  @Column({ name: 'raw_data', type: 'jsonb', nullable: true })
  rawData?: Record<string, unknown>;

  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt: Date;

  // Relations
  @ManyToOne(() => ServiceOrder, { nullable: true })
  @JoinColumn({ name: 'order_id' })
  order?: ServiceOrder;

  // @OneToMany(() => DiagnosticItem, item => item.diagnostic)
  // items: DiagnosticItem[];

  // @OneToMany(() => DiagnosticPhoto, photo => photo.diagnostic)
  // photos: DiagnosticPhoto[];

  // @OneToMany(() => DiagnosticRecommendation, rec => rec.diagnostic)
  // recommendations: DiagnosticRecommendation[];
}
