/**
 * AssetAssignment Entity - Asignaciones de Activos a Proyectos
 *
 * Registro de movimientos de activos entre obras/proyectos.
 *
 * @module Assets (MAE-015)
 */

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

@Entity('asset_assignments', { schema: 'assets' })
@Index(['tenantId', 'assetId'])
@Index(['tenantId', 'projectId'])
@Index(['tenantId', 'isCurrent'])
export class AssetAssignment {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  // Activo y proyecto
  @Column({ name: 'asset_id', type: 'uuid' })
  assetId!: string;

  @ManyToOne(() => Asset, (asset) => asset.assignments)
  @JoinColumn({ name: 'asset_id' })
  asset!: Asset;

  @Column({ name: 'project_id', type: 'uuid' })
  projectId!: string;

  @Column({ name: 'project_code', length: 50, nullable: true })
  projectCode?: string;

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

  // Periodo de asignacion
  @Column({ name: 'start_date', type: 'date' })
  startDate!: Date;

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

  @Column({ name: 'is_current', type: 'boolean', default: true })
  isCurrent!: boolean;

  // Ubicacion especifica en obra
  @Column({ name: 'location_in_project', length: 255, nullable: true })
  locationInProject?: string;

  @Column({ type: 'decimal', precision: 10, scale: 8, nullable: true })
  latitude?: number;

  @Column({ type: 'decimal', precision: 11, scale: 8, nullable: true })
  longitude?: number;

  // Operador asignado
  @Column({ name: 'operator_id', type: 'uuid', nullable: true })
  operatorId?: string;

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

  // Responsable
  @Column({ name: 'responsible_id', type: 'uuid', nullable: true })
  responsibleId?: string;

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

  // Metricas al inicio/fin
  @Column({ name: 'hours_at_start', type: 'decimal', precision: 12, scale: 2, nullable: true })
  hoursAtStart?: number;

  @Column({ name: 'hours_at_end', type: 'decimal', precision: 12, scale: 2, nullable: true })
  hoursAtEnd?: number;

  @Column({ name: 'kilometers_at_start', type: 'decimal', precision: 12, scale: 2, nullable: true })
  kilometersAtStart?: number;

  @Column({ name: 'kilometers_at_end', type: 'decimal', precision: 12, scale: 2, nullable: true })
  kilometersAtEnd?: number;

  // Tarifas
  @Column({ name: 'daily_rate', type: 'decimal', precision: 18, scale: 2, nullable: true })
  dailyRate?: number;

  @Column({ name: 'hourly_rate', type: 'decimal', precision: 18, scale: 2, nullable: true })
  hourlyRate?: number;

  // Razon de transferencia
  @Column({ name: 'transfer_reason', type: 'text', nullable: true })
  transferReason?: string;

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

  // Documento de entrega
  @Column({ name: 'delivery_document_url', length: 500, nullable: true })
  deliveryDocumentUrl?: string;

  @Column({ name: 'return_document_url', length: 500, nullable: true })
  returnDocumentUrl?: string;

  // Metadatos
  @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;
}
