/**
 * BidDocument Entity - Documentos de Licitación
 * Almacena documentos asociados a una licitación.
 *
 * @module Bidding (MAI-018)
 * @table bidding.bid_documents
 * @ddl schemas/XX-bidding-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 { Tender } from './tender.entity';

/** Type/category of document */
export type BidDocumentType =
  | 'bases'
  | 'technical_annex'
  | 'economic_annex'
  | 'clarification'
  | 'proposal_tech'
  | 'proposal_econ'
  | 'contract'
  | 'other';

@Entity({ schema: 'bidding', name: 'bid_documents' })
@Index(['tenantId'])
@Index(['tenantId', 'tenderId'])
@Index(['tenantId', 'documentType'])
@Index(['uploadedAt'])
export class BidDocument {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  /** Reference to the tender */
  @Column({ name: 'tender_id', type: 'uuid' })
  tenderId: string;

  @Column({
    name: 'document_type',
    type: 'varchar',
    length: 50,
  })
  documentType: BidDocumentType;

  /** Document name */
  @Column({ type: 'varchar', length: 255 })
  name: string;

  /** Document description */
  @Column({ type: 'text', nullable: true })
  description: string;

  /** URL/path to the file */
  @Column({ name: 'file_url', type: 'varchar', length: 500 })
  fileUrl: string;

  /** File size in bytes */
  @Column({ name: 'file_size', type: 'bigint' })
  fileSize: string;

  /** MIME type of the file */
  @Column({ name: 'mime_type', type: 'varchar', length: 100 })
  mimeType: string;

  /** Version number */
  @Column({ type: 'int', default: 1 })
  version: number;

  /** User who uploaded the document */
  @Column({ name: 'uploaded_by', type: 'uuid' })
  uploadedById: string;

  /** Timestamp when document was uploaded */
  @Column({ name: 'uploaded_at', type: 'timestamptz' })
  uploadedAt: Date;

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

  @ManyToOne(() => Tender, (tender) => tender.documents)
  @JoinColumn({ name: 'tender_id' })
  tender: Tender;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'uploaded_by' })
  uploadedBy: User;

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

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

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'created_by' })
  createdBy: User;

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

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

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'updated_by' })
  updatedBy: User;

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