/**
 * AvanceObra Entity
 * Registro de avances fisicos de obra por lote/departamento
 *
 * @module Progress
 * @table construction.avances_obra
 * @ddl schemas/01-construction-schema-ddl.sql
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
  Check,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { User } from '../../core/entities/user.entity';
import { Concepto } from '../../budgets/entities/concepto.entity';
import { FotoAvance } from './foto-avance.entity';

export type AdvanceStatus = 'pending' | 'captured' | 'reviewed' | 'approved' | 'rejected';

@Entity({ schema: 'construction', name: 'avances_obra' })
@Index(['tenantId'])
@Index(['loteId'])
@Index(['conceptoId'])
@Index(['captureDate'])
@Check(`"lote_id" IS NOT NULL AND "departamento_id" IS NULL OR "lote_id" IS NULL AND "departamento_id" IS NOT NULL`)
export class AvanceObra {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

  @Column({ name: 'capture_date', type: 'date' })
  captureDate: Date;

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

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

  @Column({
    type: 'enum',
    enum: ['pending', 'captured', 'reviewed', 'approved', 'rejected'],
    enumName: 'construction.advance_status',
    default: 'pending',
  })
  status: AdvanceStatus;

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

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

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

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

  @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(() => Concepto)
  @JoinColumn({ name: 'concepto_id' })
  concepto: Concepto;

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

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

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

  @OneToMany(() => FotoAvance, (f) => f.avance)
  fotos: FotoAvance[];
}
