import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  DeleteDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';

/**
 * Sales Order Entity
 *
 * Synchronized with DDL: database/ddl/22-sales.sql
 * Table: sales.sales_orders
 *
 * Represents confirmed sales orders with full order-to-delivery tracking.
 */
@Entity({ name: 'sales_orders', schema: 'sales' })
export class SalesOrder {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  // Order identification
  @Index()
  @Column({ name: 'order_number', type: 'varchar', length: 30 })
  orderNumber: string;

  // Origin (from quotation)
  @Column({ name: 'quotation_id', type: 'uuid', nullable: true })
  quotationId: string | null;

  // Customer
  @Index()
  @Column({ name: 'partner_id', type: 'uuid' })
  partnerId: string;

  @Column({ name: 'partner_name', type: 'varchar', length: 200, nullable: true })
  partnerName: string | null;

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

  // Addresses (JSONB)
  @Column({ name: 'billing_address', type: 'jsonb', nullable: true })
  billingAddress: Record<string, unknown> | null;

  @Column({ name: 'shipping_address', type: 'jsonb', nullable: true })
  shippingAddress: Record<string, unknown> | null;

  // Dates
  @Column({ name: 'order_date', type: 'date', default: () => 'CURRENT_DATE' })
  orderDate: Date;

  @Column({ name: 'requested_date', type: 'date', nullable: true })
  requestedDate: Date | null;

  @Column({ name: 'promised_date', type: 'date', nullable: true })
  promisedDate: Date | null;

  @Column({ name: 'shipped_date', type: 'date', nullable: true })
  shippedDate: Date | null;

  @Column({ name: 'delivered_date', type: 'date', nullable: true })
  deliveredDate: Date | null;

  // Sales rep
  @Column({ name: 'sales_rep_id', type: 'uuid', nullable: true })
  salesRepId: string | null;

  // Warehouse
  @Column({ name: 'warehouse_id', type: 'uuid', nullable: true })
  warehouseId: string | null;

  // Totals
  @Column({ type: 'varchar', length: 3, default: 'MXN' })
  currency: string;

  @Column({ type: 'decimal', precision: 15, scale: 2, default: 0 })
  subtotal: number;

  @Column({ name: 'tax_amount', type: 'decimal', precision: 15, scale: 2, default: 0 })
  taxAmount: number;

  @Column({ name: 'discount_amount', type: 'decimal', precision: 15, scale: 2, default: 0 })
  discountAmount: number;

  @Column({ name: 'shipping_amount', type: 'decimal', precision: 15, scale: 2, default: 0 })
  shippingAmount: number;

  @Column({ type: 'decimal', precision: 15, scale: 2, default: 0 })
  total: number;

  // Payment terms
  @Column({ name: 'payment_term_days', type: 'int', default: 0 })
  paymentTermDays: number;

  @Column({ name: 'payment_method', type: 'varchar', length: 50, nullable: true })
  paymentMethod: string | null;

  // Status
  @Index()
  @Column({ type: 'varchar', length: 20, default: 'draft' })
  status: 'draft' | 'confirmed' | 'processing' | 'shipped' | 'delivered' | 'cancelled';

  // Shipping
  @Column({ name: 'shipping_method', type: 'varchar', length: 50, nullable: true })
  shippingMethod: string | null;

  @Column({ name: 'tracking_number', type: 'varchar', length: 100, nullable: true })
  trackingNumber: string | null;

  @Column({ type: 'varchar', length: 100, nullable: true })
  carrier: string | null;

  // Notes
  @Column({ type: 'text', nullable: true })
  notes: string | null;

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

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

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

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

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

  @DeleteDateColumn({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt: Date | null;
}
