/**
 * ToolLoan Entity - Prestamos de Herramientas
 *
 * Registro de prestamos de herramientas entre obras o a empleados.
 *
 * @module Assets (MAE-015)
 * @table assets.tool_loans
 * @gap GAP-002
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Asset } from './asset.entity';

export type LoanStatus = 'active' | 'returned' | 'overdue' | 'lost' | 'damaged';

@Entity({ schema: 'assets', name: 'tool_loans' })
@Index(['tenantId'])
@Index(['tenantId', 'toolId'])
@Index(['tenantId', 'employeeId'])
@Index(['tenantId', 'status'])
@Index(['tenantId', 'fraccionamientoOrigenId'])
@Index(['tenantId', 'fraccionamientoDestinoId'])
export class ToolLoan {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  // Herramienta prestada
  @Column({ name: 'tool_id', type: 'uuid' })
  toolId!: string;

  @ManyToOne(() => Asset)
  @JoinColumn({ name: 'tool_id' })
  tool?: Asset;

  // Quien recibe el prestamo
  @Column({ name: 'employee_id', type: 'uuid' })
  employeeId!: string;

  @Column({ name: 'employee_name', length: 255, nullable: true })
  employeeName?: string;

  // Origen del prestamo
  @Column({ name: 'fraccionamiento_origen_id', type: 'uuid', nullable: true })
  fraccionamientoOrigenId?: string;

  @Column({ name: 'fraccionamiento_origen_name', length: 255, nullable: true })
  fraccionamientoOrigenName?: string;

  // Destino del prestamo
  @Column({ name: 'fraccionamiento_destino_id', type: 'uuid', nullable: true })
  fraccionamientoDestinoId?: string;

  @Column({ name: 'fraccionamiento_destino_name', length: 255, nullable: true })
  fraccionamientoDestinoName?: string;

  // Fechas
  @Column({ name: 'loan_date', type: 'date' })
  loanDate!: Date;

  @Column({ name: 'expected_return_date', type: 'date', nullable: true })
  expectedReturnDate?: Date;

  @Column({ name: 'actual_return_date', type: 'date', nullable: true })
  actualReturnDate?: Date;

  // Estado
  @Column({
    type: 'enum',
    enum: ['active', 'returned', 'overdue', 'lost', 'damaged'],
    enumName: 'loan_status',
    default: 'active',
  })
  status!: LoanStatus;

  // Condicion de salida
  @Column({ name: 'condition_out', type: 'text', nullable: true })
  conditionOut?: string;

  @Column({ name: 'condition_out_photos', type: 'jsonb', nullable: true })
  conditionOutPhotos?: string[];

  // Condicion de entrada
  @Column({ name: 'condition_in', type: 'text', nullable: true })
  conditionIn?: string;

  @Column({ name: 'condition_in_photos', type: 'jsonb', nullable: true })
  conditionInPhotos?: string[];

  // Aprobacion
  @Column({ name: 'approved_by_id', type: 'uuid', nullable: true })
  approvedById?: string;

  @Column({ name: 'approved_by_name', length: 255, nullable: true })
  approvedByName?: string;

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

  // Devolucion
  @Column({ name: 'received_by_id', type: 'uuid', nullable: true })
  receivedById?: string;

  @Column({ name: 'received_by_name', length: 255, nullable: true })
  receivedByName?: string;

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

  @Column({ type: 'jsonb', nullable: true })
  metadata?: Record<string, any>;

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

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

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

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