/**
 * Service Order Entity
 * Mecánicas Diesel - ERP Suite
 *
 * Represents a vehicle service order in the workshop.
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
  Check,
} from 'typeorm';

// Status values for service orders
export enum ServiceOrderStatus {
  RECEIVED = 'received',
  DIAGNOSED = 'diagnosed',
  QUOTED = 'quoted',
  APPROVED = 'approved',
  IN_PROGRESS = 'in_progress',
  WAITING_PARTS = 'waiting_parts',
  COMPLETED = 'completed',
  DELIVERED = 'delivered',
  CANCELLED = 'cancelled',
}

export enum ServiceOrderPriority {
  LOW = 'low',
  NORMAL = 'normal',
  HIGH = 'high',
  URGENT = 'urgent',
}

@Entity({ name: 'service_orders', schema: 'service_management' })
@Index('idx_orders_tenant', ['tenantId'])
@Index('idx_orders_status', ['tenantId', 'status'])
@Index('idx_orders_vehicle', ['vehicleId'])
@Index('idx_orders_customer', ['customerId'])
@Index('idx_orders_assigned', ['assignedTo'])
@Check('chk_odometer', '"odometer_out" IS NULL OR "odometer_out" >= "odometer_in"')
export class ServiceOrder {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  // Identification
  @Column({ name: 'order_number', type: 'varchar', length: 20 })
  orderNumber: string;

  // Relations
  @Column({ name: 'customer_id', type: 'uuid' })
  customerId: string;

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

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

  // Assignment
  @Column({ name: 'assigned_to', type: 'uuid', nullable: true })
  assignedTo?: string;

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

  // Status
  @Column({
    type: 'varchar',
    length: 30,
    default: ServiceOrderStatus.RECEIVED,
  })
  status: ServiceOrderStatus;

  @Column({
    type: 'varchar',
    length: 20,
    default: ServiceOrderPriority.NORMAL,
  })
  priority: ServiceOrderPriority;

  // Dates
  @Column({ name: 'received_at', type: 'timestamptz', default: () => 'NOW()' })
  receivedAt: Date;

  @Column({ name: 'promised_at', type: 'timestamptz', nullable: true })
  promisedAt?: Date;

  @Column({ name: 'started_at', type: 'timestamptz', nullable: true })
  startedAt?: Date;

  @Column({ name: 'completed_at', type: 'timestamptz', nullable: true })
  completedAt?: Date;

  @Column({ name: 'delivered_at', type: 'timestamptz', nullable: true })
  deliveredAt?: Date;

  // Odometer
  @Column({ name: 'odometer_in', type: 'integer', nullable: true })
  odometerIn?: number;

  @Column({ name: 'odometer_out', type: 'integer', nullable: true })
  odometerOut?: number;

  // Symptoms
  @Column({ name: 'customer_symptoms', type: 'text', nullable: true })
  customerSymptoms?: string;

  // Totals
  @Column({ name: 'labor_total', type: 'decimal', precision: 12, scale: 2, default: 0 })
  laborTotal: number;

  @Column({ name: 'parts_total', type: 'decimal', precision: 12, scale: 2, default: 0 })
  partsTotal: number;

  @Column({ name: 'discount_amount', type: 'decimal', precision: 12, scale: 2, default: 0 })
  discountAmount: number;

  @Column({ name: 'discount_percent', type: 'decimal', precision: 5, scale: 2, default: 0 })
  discountPercent: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0 })
  tax: number;

  @Column({ name: 'grand_total', type: 'decimal', precision: 12, scale: 2, default: 0 })
  grandTotal: number;

  // Notes
  @Column({ name: 'internal_notes', type: 'text', nullable: true })
  internalNotes?: string;

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

  // Audit
  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdBy?: string;

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

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

  // Relations (to be added when other entities are defined)
  // @ManyToOne(() => Vehicle, vehicle => vehicle.serviceOrders)
  // @JoinColumn({ name: 'vehicle_id' })
  // vehicle: Vehicle;

  // @OneToMany(() => OrderItem, item => item.order)
  // items: OrderItem[];
}
