/**
 * ComparativoProveedor Entity
 * Proveedores participantes en comparativo
 *
 * @module Purchase
 * @table purchase.comparativo_proveedores
 * @ddl schemas/07-purchase-ext-schema-ddl.sql
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { User } from '../../core/entities/user.entity';
import { ComparativoCotizaciones } from './comparativo-cotizaciones.entity';
import { ComparativoProducto } from './comparativo-producto.entity';
import { Partner } from '../../partners/entities/partner.entity';

@Entity({ schema: 'purchase', name: 'comparativo_proveedores' })
export class ComparativoProveedor {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'comparativo_id', type: 'uuid' })
  comparativoId: string;

  @Column({ name: 'supplier_id', type: 'uuid' })
  supplierId: string;

  @Column({ name: 'quotation_number', type: 'varchar', length: 50, nullable: true })
  quotationNumber: string;

  @Column({ name: 'quotation_date', type: 'date', nullable: true })
  quotationDate: Date;

  @Column({ name: 'delivery_days', type: 'integer', nullable: true })
  deliveryDays: number;

  @Column({ name: 'payment_conditions', type: 'varchar', length: 100, nullable: true })
  paymentConditions: string;

  @Column({ name: 'total_amount', type: 'decimal', precision: 16, scale: 2, nullable: true })
  totalAmount: number;

  @Column({ name: 'is_selected', type: 'boolean', default: false })
  isSelected: boolean;

  @Column({ name: 'evaluation_notes', type: 'text', nullable: true })
  evaluationNotes: string;

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

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

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

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

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

  @ManyToOne(() => ComparativoCotizaciones, (c) => c.proveedores, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'comparativo_id' })
  comparativo: ComparativoCotizaciones;

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

  @OneToMany(() => ComparativoProducto, (cp) => cp.comparativoProveedor)
  productos: ComparativoProducto[];

  // FK a partners.partners (ERP-Core) - proveedor participante
  @ManyToOne(() => Partner, { nullable: false })
  @JoinColumn({ name: 'supplier_id' })
  supplier: Partner;
}
