/**
 * ProgramaActividad Entity
 * Actividades del programa de obra (WBS)
 *
 * @module Progress
 * @table construction.programa_actividades
 * @ddl schemas/01-construction-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 { ProgramaObra } from './programa-obra.entity';

@Entity({ schema: 'construction', name: 'programa_actividades' })
@Index(['tenantId'])
@Index(['programaId'])
export class ProgramaActividad {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'programa_id', type: 'uuid' })
  programaId: string;

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

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

  @Column({ type: 'varchar', length: 255 })
  name: string;

  @Column({ type: 'integer', default: 0 })
  sequence: number;

  @Column({ name: 'planned_start', type: 'date', nullable: true })
  plannedStart: Date | null;

  @Column({ name: 'planned_end', type: 'date', nullable: true })
  plannedEnd: Date | null;

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

  @Column({ name: 'planned_weight', type: 'decimal', precision: 8, scale: 4, default: 0 })
  plannedWeight: number;

  @Column({ name: 'wbs_code', type: 'varchar', length: 50, nullable: true })
  wbsCode: 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(() => ProgramaObra, (p) => p.actividades)
  @JoinColumn({ name: 'programa_id' })
  programa: ProgramaObra;

  @ManyToOne(() => Concepto, { nullable: true })
  @JoinColumn({ name: 'concepto_id' })
  concepto: Concepto | null;

  @ManyToOne(() => ProgramaActividad, (a) => a.children, { nullable: true })
  @JoinColumn({ name: 'parent_id' })
  parent: ProgramaActividad | null;

  @OneToMany(() => ProgramaActividad, (a) => a.parent)
  children: ProgramaActividad[];

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