/**
 * Lote Entity
 * Lotes/Terrenos individuales
 *
 * @module Construction
 */

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

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

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

  @Column({ name: 'manzana_id', type: 'uuid' })
  manzanaId: string;

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

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

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

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

  @Column({ name: 'front_m', type: 'decimal', precision: 8, scale: 2, nullable: true })
  frontM: number;

  @Column({ name: 'depth_m', type: 'decimal', precision: 8, scale: 2, nullable: true })
  depthM: 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;

  // Relations
  @ManyToOne(() => Manzana, (m) => m.lotes, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'manzana_id' })
  manzana: Manzana;

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