/**
 * Generador Entity
 * Numeros generadores (soporte de cantidades para estimaciones)
 *
 * @module Estimates
 * @table estimates.generadores
 * @ddl schemas/04-estimates-schema-ddl.sql
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { User } from '../../core/entities/user.entity';
import { EstimacionConcepto } from './estimacion-concepto.entity';

export type GeneratorStatus = 'draft' | 'in_progress' | 'completed' | 'approved';

@Entity({ schema: 'estimates', name: 'generadores' })
@Index(['tenantId'])
@Index(['estimacionConceptoId'])
@Index(['status'])
export class Generador {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'estimacion_concepto_id', type: 'uuid' })
  estimacionConceptoId: string;

  @Column({ name: 'generator_number', type: 'varchar', length: 30 })
  generatorNumber: string;

  @Column({ type: 'text', nullable: true })
  description: string | null;

  @Column({
    type: 'enum',
    enum: ['draft', 'in_progress', 'completed', 'approved'],
    enumName: 'estimates.generator_status',
    default: 'draft',
  })
  status: GeneratorStatus;

  @Column({ name: 'lote_id', type: 'uuid', nullable: true })
  loteId: string | null;

  @Column({ name: 'departamento_id', type: 'uuid', nullable: true })
  departamentoId: string | null;

  @Column({ name: 'location_description', type: 'varchar', length: 255, nullable: true })
  locationDescription: string | null;

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

  @Column({ type: 'text', nullable: true })
  formula: string | null;

  @Column({ name: 'photo_url', type: 'varchar', length: 500, nullable: true })
  photoUrl: string | null;

  @Column({ name: 'sketch_url', type: 'varchar', length: 500, nullable: true })
  sketchUrl: string | null;

  @Column({ name: 'captured_by', type: 'uuid' })
  capturedById: string;

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

  @Column({ name: 'approved_by', type: 'uuid', nullable: true })
  approvedById: string | null;

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

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

  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdById: string | null;

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

  @Column({ name: 'updated_by', type: 'uuid', nullable: true })
  updatedById: string | null;

  @Column({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt: Date | null;

  @Column({ name: 'deleted_by', type: 'uuid', nullable: true })
  deletedById: string | null;

  // Relations
  @ManyToOne(() => Tenant)
  @JoinColumn({ name: 'tenant_id' })
  tenant: Tenant;

  @ManyToOne(() => EstimacionConcepto, (ec) => ec.generadores)
  @JoinColumn({ name: 'estimacion_concepto_id' })
  estimacionConcepto: EstimacionConcepto;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'captured_by' })
  capturedBy: User;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'approved_by' })
  approvedBy: User | null;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'created_by' })
  createdBy: User | null;
}
