/**
 * Departamento Entity
 * Departamentos/Unidades en torre (vivienda vertical)
 *
 * @module Construction
 * @table construction.departamentos
 * @ddl schemas/01-construction-schema-ddl.sql
 */

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

@Entity({ schema: 'construction', name: 'departamentos' })
@Index(['nivelId', 'code'], { unique: true })
@Index(['tenantId'])
@Index(['nivelId'])
@Index(['status'])
export class Departamento {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'nivel_id', type: 'uuid' })
  nivelId: string;

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

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

  @Column({ name: 'unit_number', type: 'varchar', length: 20 })
  unitNumber: string;

  @Column({ name: 'area_m2', type: 'decimal', precision: 10, scale: 2, nullable: true })
  areaM2: number;

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

  @Column({ name: 'price_base', type: 'decimal', precision: 14, scale: 2, nullable: true })
  priceBase: number;

  @Column({ name: 'price_final', type: 'decimal', precision: 14, scale: 2, nullable: true })
  priceFinal: number;

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

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

  @Column({ name: 'delivery_date', type: 'date', nullable: true })
  deliveryDate: 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;

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

  // Relations
  @ManyToOne(() => Nivel, (n) => n.departamentos, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'nivel_id' })
  nivel: Nivel;

  @ManyToOne(() => Prototipo)
  @JoinColumn({ name: 'prototipo_id' })
  prototipo: Prototipo;
}
