/**
 * Anticipo Entity
 * Anticipos otorgados a subcontratistas
 *
 * @module Estimates
 * @table estimates.anticipos
 * @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 { Amortizacion } from './amortizacion.entity';

export type AdvanceType = 'initial' | 'progress' | 'materials';

@Entity({ schema: 'estimates', name: 'anticipos' })
@Index(['tenantId', 'advanceNumber'], { unique: true })
@Index(['tenantId'])
@Index(['contratoId'])
@Index(['advanceType'])
export class Anticipo {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'contrato_id', type: 'uuid' })
  contratoId: string;

  @Column({
    name: 'advance_type',
    type: 'enum',
    enum: ['initial', 'progress', 'materials'],
    enumName: 'estimates.advance_type',
    default: 'initial',
  })
  advanceType: AdvanceType;

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

  @Column({ name: 'advance_date', type: 'date' })
  advanceDate: Date;

  @Column({ name: 'gross_amount', type: 'decimal', precision: 16, scale: 2 })
  grossAmount: number;

  @Column({ name: 'tax_amount', type: 'decimal', precision: 16, scale: 2, default: 0 })
  taxAmount: number;

  @Column({ name: 'net_amount', type: 'decimal', precision: 16, scale: 2 })
  netAmount: number;

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

  @Column({ name: 'amortized_amount', type: 'decimal', precision: 16, scale: 2, default: 0 })
  amortizedAmount: number;

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

  @Column({ name: 'is_fully_amortized', type: 'boolean', default: false })
  isFullyAmortized: boolean;

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

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

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

  @Column({ name: 'payment_reference', type: 'varchar', length: 100, nullable: true })
  paymentReference: string | null;

  @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(() => User)
  @JoinColumn({ name: 'approved_by' })
  approvedBy: User | null;

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

  @OneToMany(() => Amortizacion, (a) => a.anticipo)
  amortizaciones: Amortizacion[];
}
