/**
 * Manzana Entity
 * Manzanas (bloques) dentro de una etapa
 *
 * @module Construction
 */

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

@Entity({ schema: 'construction', name: 'manzanas' })
@Index(['etapaId', 'code'], { unique: true })
export class Manzana {
  @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, nullable: true })
  name: string;

  @Column({ name: 'total_lots', type: 'integer', default: 0 })
  totalLots: 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;

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

  @OneToMany(() => Lote, (l) => l.manzana)
  lotes: Lote[];
}
