/**
 * BidCalendar Entity - Calendario de Licitación
 *
 * Eventos y fechas importantes del proceso de 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 CalendarEventType =
  | 'publication'
  | 'site_visit'
  | 'clarification_meeting'
  | 'clarification_deadline'
  | 'submission_deadline'
  | 'opening'
  | 'technical_evaluation'
  | 'economic_evaluation'
  | 'award_notification'
  | 'contract_signing'
  | 'kick_off'
  | 'milestone'
  | 'internal_review'
  | 'team_meeting'
  | 'reminder'
  | 'other';

export type EventPriority = 'low' | 'medium' | 'high' | 'critical';

export type EventStatus = 'scheduled' | 'in_progress' | 'completed' | 'cancelled' | 'postponed';

@Entity('bid_calendar', { schema: 'bidding' })
@Index(['tenantId', 'bidId'])
@Index(['tenantId', 'eventDate'])
@Index(['tenantId', 'eventType'])
export class BidCalendar {
  @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.calendarEvents)
  @JoinColumn({ name: 'bid_id' })
  bid?: Bid;

  // Información del evento
  @Column({ length: 255 })
  title!: string;

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

  @Column({
    name: 'event_type',
    type: 'enum',
    enum: ['publication', 'site_visit', 'clarification_meeting', 'clarification_deadline', 'submission_deadline', 'opening', 'technical_evaluation', 'economic_evaluation', 'award_notification', 'contract_signing', 'kick_off', 'milestone', 'internal_review', 'team_meeting', 'reminder', 'other'],
    enumName: 'calendar_event_type',
  })
  eventType!: CalendarEventType;

  @Column({
    type: 'enum',
    enum: ['low', 'medium', 'high', 'critical'],
    enumName: 'event_priority',
    default: 'medium',
  })
  priority!: EventPriority;

  @Column({
    type: 'enum',
    enum: ['scheduled', 'in_progress', 'completed', 'cancelled', 'postponed'],
    enumName: 'event_status',
    default: 'scheduled',
  })
  status!: EventStatus;

  // Fechas y hora
  @Column({ name: 'event_date', type: 'timestamptz' })
  eventDate!: Date;

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

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

  @Column({ name: 'timezone', length: 50, default: 'America/Mexico_City' })
  timezone!: string;

  // Ubicación
  @Column({ length: 255, nullable: true })
  location?: string;

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

  @Column({ name: 'meeting_link', length: 500, nullable: true })
  meetingLink?: string;

  // Recordatorios
  @Column({ name: 'reminder_minutes', type: 'int', array: true, nullable: true })
  reminderMinutes?: number[];

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

  @Column({ name: 'last_reminder_at', type: 'timestamptz', nullable: true })
  lastReminderAt?: Date;

  // Asignación
  @Column({ name: 'assigned_to_id', type: 'uuid', nullable: true })
  assignedToId?: string;

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'assigned_to_id' })
  assignedTo?: User;

  @Column({ name: 'attendees', type: 'uuid', array: true, nullable: true })
  attendees?: string[];

  // Resultado del evento
  @Column({ name: 'outcome', type: 'text', nullable: true })
  outcome?: string;

  @Column({ name: 'action_items', type: 'jsonb', nullable: true })
  actionItems?: Record<string, any>[];

  // Recurrencia
  @Column({ name: 'is_recurring', type: 'boolean', default: false })
  isRecurring!: boolean;

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

  @Column({ name: 'parent_event_id', type: 'uuid', nullable: true })
  parentEventId?: string;

  // Flags
  @Column({ name: 'is_mandatory', type: 'boolean', default: false })
  isMandatory!: boolean;

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

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

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