/**
 * EstimacionConcepto Entity
 * Lineas de concepto por estimacion
 *
 * @module Estimates
 * @table estimates.estimacion_conceptos
 * @ddl schemas/04-estimates-schema-ddl.sql
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { User } from '../../core/entities/user.entity';
import { Concepto } from '../../budgets/entities/concepto.entity';
import { Estimacion } from './estimacion.entity';
import { Generador } from './generador.entity';

@Entity({ schema: 'estimates', name: 'estimacion_conceptos' })
@Index(['estimacionId', 'conceptoId'], { unique: true })
@Index(['tenantId'])
@Index(['estimacionId'])
@Index(['conceptoId'])
export class EstimacionConcepto {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'estimacion_id', type: 'uuid' })
  estimacionId: string;

  @Column({ name: 'concepto_id', type: 'uuid' })
  conceptoId: string;

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

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

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

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

  // Columna calculada (GENERATED ALWAYS AS) - solo lectura
  @Column({
    name: 'quantity_accumulated',
    type: 'decimal',
    precision: 12,
    scale: 4,
    insert: false,
    update: false,
  })
  quantityAccumulated: number;

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

  // Columna calculada (GENERATED ALWAYS AS) - solo lectura
  @Column({
    name: 'amount_current',
    type: 'decimal',
    precision: 14,
    scale: 2,
    insert: false,
    update: false,
  })
  amountCurrent: number;

  @Column({ type: 'text', nullable: true })
  notes: string | 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(() => Estimacion, (e) => e.conceptos)
  @JoinColumn({ name: 'estimacion_id' })
  estimacion: Estimacion;

  @ManyToOne(() => Concepto)
  @JoinColumn({ name: 'concepto_id' })
  concepto: Concepto;

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

  @OneToMany(() => Generador, (g) => g.estimacionConcepto)
  generadores: Generador[];
}
