/**
 * BidTeam Entity - Equipo de Licitación
 *
 * Miembros del equipo asignados 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 TeamRole =
  | 'bid_manager'
  | 'technical_lead'
  | 'cost_engineer'
  | 'legal_advisor'
  | 'commercial_manager'
  | 'project_manager'
  | 'quality_manager'
  | 'hse_manager'
  | 'procurement_lead'
  | 'design_lead'
  | 'reviewer'
  | 'contributor'
  | 'support';

export type MemberStatus = 'active' | 'inactive' | 'pending' | 'removed';

@Entity('bid_team', { schema: 'bidding' })
@Index(['tenantId', 'bidId'])
@Index(['tenantId', 'userId'])
export class BidTeam {
  @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.teamMembers)
  @JoinColumn({ name: 'bid_id' })
  bid?: Bid;

  // Referencia a usuario
  @Column({ name: 'user_id', type: 'uuid' })
  userId!: string;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'user_id' })
  user?: User;

  // Rol y responsabilidades
  @Column({
    type: 'enum',
    enum: ['bid_manager', 'technical_lead', 'cost_engineer', 'legal_advisor', 'commercial_manager', 'project_manager', 'quality_manager', 'hse_manager', 'procurement_lead', 'design_lead', 'reviewer', 'contributor', 'support'],
    enumName: 'bid_team_role',
  })
  role!: TeamRole;

  @Column({
    type: 'enum',
    enum: ['active', 'inactive', 'pending', 'removed'],
    enumName: 'bid_team_status',
    default: 'active',
  })
  status!: MemberStatus;

  @Column({ type: 'text', array: true, nullable: true })
  responsibilities?: string[];

  // Dedicación
  @Column({
    name: 'allocation_percentage',
    type: 'decimal',
    precision: 5,
    scale: 2,
    default: 100,
  })
  allocationPercentage!: number;

  @Column({ name: 'estimated_hours', type: 'decimal', precision: 8, scale: 2, nullable: true })
  estimatedHours?: number;

  @Column({ name: 'actual_hours', type: 'decimal', precision: 8, scale: 2, default: 0 })
  actualHours!: number;

  // Fechas de participación
  @Column({ name: 'start_date', type: 'date' })
  startDate!: Date;

  @Column({ name: 'end_date', type: 'date', nullable: true })
  endDate?: Date;

  // Permisos específicos
  @Column({ name: 'can_edit_technical', type: 'boolean', default: false })
  canEditTechnical!: boolean;

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

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

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

  // Notificaciones
  @Column({ name: 'receive_notifications', type: 'boolean', default: true })
  receiveNotifications!: boolean;

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

  // Evaluación de participación
  @Column({
    name: 'performance_rating',
    type: 'decimal',
    precision: 3,
    scale: 2,
    nullable: true,
  })
  performanceRating?: number;

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

  // Información de contacto externa (si no es empleado)
  @Column({ name: 'is_external', type: 'boolean', default: false })
  isExternal!: boolean;

  @Column({ name: 'external_company', length: 255, nullable: true })
  externalCompany?: string;

  @Column({ name: 'external_email', length: 255, nullable: true })
  externalEmail?: string;

  @Column({ name: 'external_phone', length: 50, nullable: true })
  externalPhone?: string;

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

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

  // Auditoría
  @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;
}
