/**
 * BidDocument Entity - Documentos de Licitación
 *
 * Almacena documentos asociados a una licitación.
 *
 * @module Bidding
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { User } from '../../core/entities/user.entity';
import { Bid } from './bid.entity';

export type DocumentCategory =
  | 'tender_bases'
  | 'clarifications'
  | 'annexes'
  | 'technical_proposal'
  | 'economic_proposal'
  | 'legal_documents'
  | 'experience_certificates'
  | 'financial_statements'
  | 'bonds'
  | 'contracts'
  | 'correspondence'
  | 'meeting_minutes'
  | 'other';

export type DocumentStatus = 'draft' | 'pending_review' | 'approved' | 'rejected' | 'submitted' | 'archived';

@Entity('bid_documents', { schema: 'bidding' })
@Index(['tenantId', 'bidId'])
@Index(['tenantId', 'category'])
export class BidDocument {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  // Referencia a licitación
  @Column({ name: 'bid_id', type: 'uuid' })
  bidId!: string;

  @ManyToOne(() => Bid, (bid) => bid.documents)
  @JoinColumn({ name: 'bid_id' })
  bid?: Bid;

  // Información del documento
  @Column({ length: 255 })
  name!: string;

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

  @Column({
    type: 'enum',
    enum: ['tender_bases', 'clarifications', 'annexes', 'technical_proposal', 'economic_proposal', 'legal_documents', 'experience_certificates', 'financial_statements', 'bonds', 'contracts', 'correspondence', 'meeting_minutes', 'other'],
    enumName: 'bid_document_category',
  })
  category!: DocumentCategory;

  @Column({
    type: 'enum',
    enum: ['draft', 'pending_review', 'approved', 'rejected', 'submitted', 'archived'],
    enumName: 'bid_document_status',
    default: 'draft',
  })
  status!: DocumentStatus;

  // Archivo
  @Column({ name: 'file_path', length: 500 })
  filePath!: string;

  @Column({ name: 'file_name', length: 255 })
  fileName!: string;

  @Column({ name: 'file_type', length: 100 })
  fileType!: string;

  @Column({ name: 'file_size', type: 'bigint' })
  fileSize!: number;

  @Column({ name: 'mime_type', length: 100, nullable: true })
  mimeType?: string;

  // Versión
  @Column({ type: 'int', default: 1 })
  version!: number;

  @Column({ name: 'is_current_version', type: 'boolean', default: true })
  isCurrentVersion!: boolean;

  @Column({ name: 'previous_version_id', type: 'uuid', nullable: true })
  previousVersionId?: string;

  // Metadatos de revisión
  @Column({ name: 'reviewed_by_id', type: 'uuid', nullable: true })
  reviewedById?: string;

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'reviewed_by_id' })
  reviewedBy?: User;

  @Column({ name: 'reviewed_at', type: 'timestamptz', nullable: true })
  reviewedAt?: Date;

  @Column({ name: 'review_comments', type: 'text', nullable: true })
  reviewComments?: string;

  // Flags
  @Column({ name: 'is_required', type: 'boolean', default: false })
  isRequired!: boolean;

  @Column({ name: 'is_confidential', type: 'boolean', default: false })
  isConfidential!: boolean;

  @Column({ name: 'is_submitted', type: 'boolean', default: false })
  isSubmitted!: boolean;

  @Column({ name: 'submitted_at', type: 'timestamptz', nullable: true })
  submittedAt?: Date;

  // Fecha de vencimiento (para documentos con vigencia)
  @Column({ name: 'expiry_date', type: 'date', nullable: true })
  expiryDate?: Date;

  // Hash para verificación de integridad
  @Column({ name: 'file_hash', length: 128, nullable: true })
  fileHash?: string;

  // Notas y metadatos
  @Column({ type: 'text', nullable: true })
  notes?: string;

  @Column({ type: 'jsonb', nullable: true })
  metadata?: Record<string, any>;

  // Auditoría
  @Column({ name: 'uploaded_by_id', type: 'uuid', nullable: true })
  uploadedById?: string;

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'uploaded_by_id' })
  uploadedBy?: User;

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

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

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

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

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