import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { CarrierProfileOrmEntity } from './carrier-profile.orm-entity';

/**
 * CSV Booking ORM Entity
 *
 * TypeORM entity for csv_bookings table
 * Stores booking requests made from CSV rate search results
 */
@Entity('csv_bookings')
@Index(['userId'])
@Index(['organizationId'])
@Index(['status'])
@Index(['carrierEmail'])
@Index(['confirmationToken'])
@Index(['requestedAt'])
export class CsvBookingOrmEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'user_id', type: 'uuid' })
  @Index()
  userId: string;

  @Column({ name: 'organization_id', type: 'uuid' })
  @Index()
  organizationId: string;

  @Column({ name: 'carrier_name', type: 'varchar', length: 255 })
  carrierName: string;

  @Column({ name: 'carrier_email', type: 'varchar', length: 255 })
  @Index()
  carrierEmail: string;

  @Column({ name: 'origin', type: 'varchar', length: 5 })
  origin: string;

  @Column({ name: 'destination', type: 'varchar', length: 5 })
  destination: string;

  @Column({ name: 'volume_cbm', type: 'decimal', precision: 10, scale: 2 })
  volumeCBM: number;

  @Column({ name: 'weight_kg', type: 'decimal', precision: 10, scale: 2 })
  weightKG: number;

  @Column({ name: 'pallet_count', type: 'integer' })
  palletCount: number;

  @Column({ name: 'price_usd', type: 'decimal', precision: 10, scale: 2 })
  priceUSD: number;

  @Column({ name: 'price_eur', type: 'decimal', precision: 10, scale: 2 })
  priceEUR: number;

  @Column({ name: 'primary_currency', type: 'varchar', length: 3 })
  primaryCurrency: string;

  @Column({ name: 'transit_days', type: 'integer' })
  transitDays: number;

  @Column({ name: 'container_type', type: 'varchar', length: 50 })
  containerType: string;

  @Column({
    name: 'status',
    type: 'enum',
    enum: [
      'PENDING_PAYMENT',
      'PENDING_BANK_TRANSFER',
      'PENDING',
      'ACCEPTED',
      'REJECTED',
      'CANCELLED',
    ],
    default: 'PENDING_PAYMENT',
  })
  @Index()
  status:
    | 'PENDING_PAYMENT'
    | 'PENDING_BANK_TRANSFER'
    | 'PENDING'
    | 'ACCEPTED'
    | 'REJECTED'
    | 'CANCELLED';

  @Column({ name: 'documents', type: 'jsonb' })
  documents: Array<{
    id: string;
    type: string;
    fileName: string;
    filePath: string;
    mimeType: string;
    size: number;
    uploadedAt: Date;
  }>;

  @Column({ name: 'confirmation_token', type: 'varchar', length: 255, unique: true })
  @Index()
  confirmationToken: string;

  @Column({ name: 'booking_number', type: 'varchar', length: 20, nullable: true })
  @Index()
  bookingNumber: string | null;

  @Column({ name: 'password_hash', type: 'text', nullable: true })
  passwordHash: string | null;

  @Column({ name: 'requested_at', type: 'timestamp with time zone' })
  @Index()
  requestedAt: Date;

  @Column({ name: 'responded_at', type: 'timestamp with time zone', nullable: true })
  respondedAt?: Date;

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

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

  // Carrier Relations
  @Column({ name: 'carrier_id', type: 'uuid', nullable: true })
  carrierId: string | null;

  @ManyToOne(() => CarrierProfileOrmEntity, carrier => carrier.bookings, {
    onDelete: 'SET NULL',
  })
  @JoinColumn({ name: 'carrier_id' })
  carrierProfile: CarrierProfileOrmEntity | null;

  @Column({ name: 'carrier_viewed_at', type: 'timestamp', nullable: true })
  carrierViewedAt: Date | null;

  @Column({ name: 'carrier_accepted_at', type: 'timestamp', nullable: true })
  carrierAcceptedAt: Date | null;

  @Column({ name: 'carrier_rejected_at', type: 'timestamp', nullable: true })
  carrierRejectedAt: Date | null;

  @Column({ name: 'carrier_rejection_reason', type: 'text', nullable: true })
  carrierRejectionReason: string | null;

  @Column({ name: 'carrier_notes', type: 'text', nullable: true })
  carrierNotes: string | null;

  @Column({ name: 'stripe_payment_intent_id', type: 'varchar', length: 255, nullable: true })
  stripePaymentIntentId: string | null;

  @Column({ name: 'commission_rate', type: 'decimal', precision: 5, scale: 2, nullable: true })
  commissionRate: number | null;

  @Column({
    name: 'commission_amount_eur',
    type: 'decimal',
    precision: 12,
    scale: 2,
    nullable: true,
  })
  commissionAmountEur: number | null;

  @CreateDateColumn({ name: 'created_at', type: 'timestamp with time zone' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp with time zone' })
  updatedAt: Date;
}
