/**
 * Nivel Entity
 * Pisos/Niveles de una torre
 *
 * @module Construction
 * @table construction.niveles
 * @ddl schemas/01-construction-schema-ddl.sql
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
} from 'typeorm';
import { Torre } from './torre.entity';
import { Departamento } from './departamento.entity';

@Entity({ schema: 'construction', name: 'niveles' })
@Index(['torreId', 'floorNumber'], { unique: true })
@Index(['tenantId'])
@Index(['torreId'])
export class Nivel {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'torre_id', type: 'uuid' })
  torreId: string;

  @Column({ name: 'floor_number', type: 'integer' })
  floorNumber: number;

  @Column({ type: 'varchar', length: 50, nullable: true })
  name: string;

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

  @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(() => Torre, (t) => t.niveles, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'torre_id' })
  torre: Torre;

  @OneToMany(() => Departamento, (d) => d.nivel)
  departamentos: Departamento[];
}
