/**
 * Proposal Entity - Propuestas Enviadas
 * Representa una propuesta enviada a una licitación.
 *
 * @module Bidding (MAI-018)
 * @table bidding.proposals
 * @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';
import { Vendor } from './vendor.entity';

/** Status of the proposal */
export type ProposalStatus = 'received' | 'evaluating' | 'qualified' | 'disqualified' | 'winner';

@Entity({ schema: 'bidding', name: 'proposals' })
@Index(['tenantId'])
@Index(['tenantId', 'tenderId'])
@Index(['tenantId', 'vendorId'])
@Index(['tenantId', 'status'])
@Index(['submittedAt'])
export class Proposal {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

  /** Reference to the vendor who submitted */
  @Column({ name: 'vendor_id', type: 'uuid' })
  vendorId: string;

  /** Proposed amount in cents */
  @Column({ name: 'proposed_amount', type: 'bigint' })
  proposedAmount: string;

  /** Proposed schedule in days */
  @Column({ name: 'proposed_schedule_days', type: 'int' })
  proposedScheduleDays: number;

  /** URL to technical proposal document */
  @Column({ name: 'technical_proposal_url', type: 'varchar', length: 500, nullable: true })
  technicalProposalUrl: string;

  /** URL to economic proposal document */
  @Column({ name: 'economic_proposal_url', type: 'varchar', length: 500, nullable: true })
  economicProposalUrl: string;

  /** Technical evaluation score */
  @Column({
    name: 'technical_score',
    type: 'decimal',
    precision: 5,
    scale: 2,
    nullable: true,
  })
  technicalScore: number;

  /** Economic evaluation score */
  @Column({
    name: 'economic_score',
    type: 'decimal',
    precision: 5,
    scale: 2,
    nullable: true,
  })
  economicScore: number;

  /** Total combined score */
  @Column({
    name: 'total_score',
    type: 'decimal',
    precision: 5,
    scale: 2,
    nullable: true,
  })
  totalScore: number;

  @Column({
    type: 'varchar',
    length: 30,
    default: 'received',
  })
  status: ProposalStatus;

  /** Timestamp when the proposal was submitted */
  @Column({ name: 'submitted_at', type: 'timestamptz' })
  submittedAt: Date;

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

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

  @ManyToOne(() => Vendor, (vendor) => vendor.proposals)
  @JoinColumn({ name: 'vendor_id' })
  vendor: Vendor;

  // 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;
}
