/**
 * BitacoraObra Entity
 * Registro diario de bitacora de obra
 *
 * @module Progress
 * @table construction.bitacora_obra
 * @ddl schemas/01-construction-schema-ddl.sql
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { User } from '../../core/entities/user.entity';
import { Fraccionamiento } from '../../construction/entities/fraccionamiento.entity';

@Entity({ schema: 'construction', name: 'bitacora_obra' })
@Index(['fraccionamientoId', 'entryNumber'], { unique: true })
@Index(['tenantId'])
@Index(['fraccionamientoId'])
export class BitacoraObra {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'fraccionamiento_id', type: 'uuid' })
  fraccionamientoId: string;

  @Column({ name: 'entry_date', type: 'date' })
  entryDate: Date;

  @Column({ name: 'entry_number', type: 'integer' })
  entryNumber: number;

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

  @Column({ name: 'temperature_max', type: 'decimal', precision: 4, scale: 1, nullable: true })
  temperatureMax: number | null;

  @Column({ name: 'temperature_min', type: 'decimal', precision: 4, scale: 1, nullable: true })
  temperatureMin: number | null;

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

  @Column({ type: 'text' })
  description: string;

  @Column({ type: 'text', nullable: true })
  observations: string | null;

  @Column({ type: 'text', nullable: true })
  incidents: string | null;

  @Column({ name: 'registered_by', type: 'uuid' })
  registeredById: string;

  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt: Date;

  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdById: string | null;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz', nullable: true })
  updatedAt: Date | null;

  @Column({ name: 'updated_by', type: 'uuid', nullable: true })
  updatedById: string | null;

  @Column({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt: Date | null;

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

  // Relations
  @ManyToOne(() => Tenant)
  @JoinColumn({ name: 'tenant_id' })
  tenant: Tenant;

  @ManyToOne(() => Fraccionamiento)
  @JoinColumn({ name: 'fraccionamiento_id' })
  fraccionamiento: Fraccionamiento;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'registered_by' })
  registeredBy: User;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'created_by' })
  createdBy: User | null;
}
