/**
 * Opportunity Entity - Oportunidades de Licitación
 * Representa oportunidades de licitación/proyecto en el pipeline comercial.
 *
 * @module Bidding (MAI-018)
 * @table bidding.opportunities
 * @ddl schemas/XX-bidding-schema-ddl.sql
 */

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

/** Source of the opportunity */
export type OpportunitySource = 'government_portal' | 'private_client' | 'referral' | 'other';

/** Status of the opportunity in the pipeline */
export type OpportunityStatus = 'registered' | 'evaluating' | 'go' | 'no_go' | 'preparing' | 'converted';

/** Priority level */
export type OpportunityPriority = 'high' | 'medium' | 'low';

@Entity({ schema: 'bidding', name: 'opportunities' })
@Index(['tenantId'])
@Index(['tenantId', 'code'], { unique: true })
@Index(['tenantId', 'status'])
@Index(['tenantId', 'source'])
@Index(['tenantId', 'priority'])
@Index(['deadlineDate'])
export class Opportunity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  /** Unique code within tenant, format: OPP-2026-001 */
  @Column({ type: 'varchar', length: 50 })
  code: string;

  @Column({ type: 'varchar', length: 255 })
  title: string;

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

  @Column({
    type: 'varchar',
    length: 50,
    default: 'other',
  })
  source: OpportunitySource;

  /** Client/organization name */
  @Column({ name: 'client_name', type: 'varchar', length: 255 })
  clientName: string;

  /** Project type: vivienda_vertical, vivienda_horizontal, urbanizacion, etc. */
  @Column({ name: 'project_type', type: 'varchar', length: 100 })
  projectType: string;

  @Column({ type: 'varchar', length: 255, nullable: true })
  location: string;

  /** Estimated amount in cents (BIGINT) */
  @Column({ name: 'estimated_amount', type: 'bigint', nullable: true })
  estimatedAmount: string;

  /** Estimated number of housing units */
  @Column({ name: 'estimated_units', type: 'int', nullable: true })
  estimatedUnits: number;

  @Column({
    type: 'varchar',
    length: 50,
    default: 'registered',
  })
  status: OpportunityStatus;

  /** Date when go/no-go decision was made */
  @Column({ name: 'go_decision_date', type: 'date', nullable: true })
  goDecisionDate: Date;

  /** Reason for go/no-go decision */
  @Column({ name: 'go_decision_reason', type: 'text', nullable: true })
  goDecisionReason: string;

  @Column({
    type: 'varchar',
    length: 20,
    default: 'medium',
  })
  priority: OpportunityPriority;

  /** Deadline for opportunity (proposal submission, etc.) */
  @Column({ name: 'deadline_date', type: 'date' })
  deadlineDate: Date;

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

  @OneToMany(() => Tender, (tender) => tender.opportunity)
  tenders: Tender[];

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