/**
 * Vendor Entity - Proveedores/Contratistas
 * Representa proveedores y contratistas que participan en licitaciones.
 *
 * @module Bidding (MAI-018)
 * @table bidding.vendors
 * @ddl schemas/XX-bidding-schema-ddl.sql
 */

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

/** Vendor certification structure */
export interface VendorCertification {
  name: string;
  issuedBy: string;
  issuedDate: string;
  expiryDate?: string;
  documentUrl?: string;
}

/** Vendor performance history entry */
export interface VendorPerformanceEntry {
  projectName: string;
  clientName: string;
  contractAmount: number;
  completedDate: string;
  rating: number;
  notes?: string;
}

@Entity({ schema: 'bidding', name: 'vendors' })
@Index(['tenantId'])
@Index(['tenantId', 'code'], { unique: true })
@Index(['tenantId', 'rfc'])
@Index(['tenantId', 'isActive'])
@Index(['rating'])
@Check('rating >= 1 AND rating <= 5')
export class Vendor {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  /** Unique code within tenant */
  @Column({ type: 'varchar', length: 20 })
  code: string;

  /** Business name (razon social) */
  @Column({ name: 'business_name', type: 'varchar', length: 255 })
  businessName: string;

  /** RFC (tax ID) */
  @Column({ type: 'varchar', length: 13, nullable: true })
  rfc: string;

  /** List of specialties */
  @Column({ type: 'text', array: true, nullable: true })
  specialties: string[];

  /** Rating from 1 to 5 */
  @Column({
    type: 'decimal',
    precision: 2,
    scale: 1,
    nullable: true,
  })
  rating: number;

  /** Certifications in JSON format */
  @Column({ type: 'jsonb', nullable: true })
  certifications: VendorCertification[];

  /** Performance history in JSON format */
  @Column({ name: 'performance_history', type: 'jsonb', nullable: true })
  performanceHistory: VendorPerformanceEntry[];

  /** Whether all documentation is valid/current */
  @Column({ name: 'documentation_valid', type: 'boolean', default: false })
  documentationValid: boolean;

  /** Contact person name */
  @Column({ name: 'contact_name', type: 'varchar', length: 255, nullable: true })
  contactName: string;

  /** Contact email */
  @Column({ name: 'contact_email', type: 'varchar', length: 255, nullable: true })
  contactEmail: string;

  /** Contact phone */
  @Column({ name: 'contact_phone', type: 'varchar', length: 50, nullable: true })
  contactPhone: string;

  /** Whether the vendor is active */
  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive: boolean;

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

  @OneToMany(() => Proposal, (proposal) => proposal.vendor)
  proposals: Proposal[];

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