/**
 * BidCalendar Entity - Fechas Clave de Licitación
 * Eventos y fechas importantes del proceso de licitación.
 *
 * @module Bidding (MAI-018)
 * @table bidding.bid_calendar
 * @ddl schemas/XX-bidding-schema-ddl.sql
 */

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

/** Type of calendar event */
export type CalendarEventType =
  | 'site_visit'
  | 'clarification_meeting'
  | 'submission_deadline'
  | 'technical_opening'
  | 'economic_opening'
  | 'award_date'
  | 'other';

@Entity({ schema: 'bidding', name: 'bid_calendar' })
@Index(['tenantId'])
@Index(['tenantId', 'tenderId'])
@Index(['tenantId', 'eventType'])
@Index(['eventDate'])
@Index(['alertSent'])
export class BidCalendar {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  /** Reference to the tender */
  @Column({ name: 'tender_id', type: 'uuid' })
  tenderId: string;

  @Column({
    name: 'event_type',
    type: 'varchar',
    length: 50,
  })
  eventType: CalendarEventType;

  /** Date and time of the event */
  @Column({ name: 'event_date', type: 'timestamptz' })
  eventDate: Date;

  /** Description of the event */
  @Column({ type: 'varchar', length: 255 })
  description: string;

  /** Days before the event to send alert */
  @Column({ name: 'alert_days_before', type: 'int', default: 3 })
  alertDaysBefore: number;

  /** Whether alert has been sent */
  @Column({ name: 'alert_sent', type: 'boolean', default: false })
  alertSent: boolean;

  /** Additional notes */
  @Column({ type: 'text', nullable: true })
  notes: string;

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

  @ManyToOne(() => Tender, (tender) => tender.calendarEvents)
  @JoinColumn({ name: 'tender_id' })
  tender: 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;
}
