/**
 * Etapa Entity
 * Etapas/Fases de un fraccionamiento
 *
 * @module Construction
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
} from 'typeorm';
import { Fraccionamiento } from './fraccionamiento.entity';
import { Manzana } from './manzana.entity';

@Entity({ schema: 'construction', name: 'etapas' })
@Index(['fraccionamientoId', 'code'], { unique: true })
export class Etapa {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'fraccionamiento_id', type: 'uuid' })
  fraccionamientoId: string;

  @Column({ type: 'varchar', length: 20 })
  code: string;

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

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

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

  @Column({ name: 'total_lots', type: 'integer', default: 0 })
  totalLots: number;

  @Column({ type: 'varchar', length: 50, default: 'draft' })
  status: string;

  @Column({ name: 'start_date', type: 'date', nullable: true })
  startDate: Date;

  @Column({ name: 'expected_end_date', type: 'date', nullable: true })
  expectedEndDate: Date;

  @Column({ name: 'actual_end_date', type: 'date', nullable: true })
  actualEndDate: Date;

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

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

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

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

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

  // Relations
  @ManyToOne(() => Fraccionamiento, (f) => f.etapas, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'fraccionamiento_id' })
  fraccionamiento: Fraccionamiento;

  @OneToMany(() => Manzana, (m) => m.etapa)
  manzanas: Manzana[];
}
