/**
 * FotoAvance Entity
 * Evidencia fotografica de avances de obra
 *
 * @module Progress
 * @table construction.fotos_avance
 * @ddl schemas/01-construction-schema-ddl.sql
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { User } from '../../core/entities/user.entity';
import { AvanceObra } from './avance-obra.entity';

@Entity({ schema: 'construction', name: 'fotos_avance' })
@Index(['tenantId'])
@Index(['avanceId'])
export class FotoAvance {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'avance_id', type: 'uuid' })
  avanceId: string;

  @Column({ name: 'file_url', type: 'varchar', length: 500 })
  fileUrl: string;

  @Column({ name: 'file_name', type: 'varchar', length: 255, nullable: true })
  fileName: string | null;

  @Column({ name: 'file_size', type: 'integer', nullable: true })
  fileSize: number | null;

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

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

  // PostGIS Point para ubicacion GPS
  @Column({
    type: 'geometry',
    spatialFeatureType: 'Point',
    srid: 4326,
    nullable: true,
  })
  location: string | null;

  @Column({ name: 'captured_at', type: 'timestamptz', default: () => 'NOW()' })
  capturedAt: Date;

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

  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdById: 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(() => AvanceObra, (a) => a.fotos)
  @JoinColumn({ name: 'avance_id' })
  avance: AvanceObra;

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