/**
 * Torre Entity
 * Torres/Edificios verticales dentro de una etapa
 *
 * @module Construction
 * @table construction.torres
 * @ddl schemas/01-construction-schema-ddl.sql
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
} from 'typeorm';
import { Etapa } from './etapa.entity';
import { Nivel } from './nivel.entity';

@Entity({ schema: 'construction', name: 'torres' })
@Index(['etapaId', 'code'], { unique: true })
@Index(['tenantId'])
@Index(['etapaId'])
export class Torre {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'etapa_id', type: 'uuid' })
  etapaId: string;

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

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

  @Column({ name: 'total_floors', type: 'integer', default: 1 })
  totalFloors: number;

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

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

  @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;

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

  // Relations
  @ManyToOne(() => Etapa, (e) => e.torres, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'etapa_id' })
  etapa: Etapa;

  @OneToMany(() => Nivel, (n) => n.torre)
  niveles: Nivel[];
}
