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

/**
 * Sales Order Entity
 *
 * Aligned with SQL schema used by orders.service.ts
 * Supports full Order-to-Cash flow with:
 * - PaymentTerms integration
 * - Automatic picking creation
 * - Stock reservation
 * - Invoice and delivery status tracking
 */
@Entity({ name: 'sales_orders', schema: 'sales' })
export class SalesOrder {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Index()
  @Column({ name: 'company_id', type: 'uuid' })
  companyId: string;

  // Order identification
  @Index()
  @Column({ type: 'varchar', length: 30 })
  name: string; // Order number (e.g., SO-000001)

  @Column({ name: 'client_order_ref', type: 'varchar', length: 100, nullable: true })
  clientOrderRef: string | null; // Customer's reference number

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

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

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

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

  @Column({ name: 'commitment_date', type: 'date', nullable: true })
  commitmentDate: Date | null; // Promised delivery date

  // Currency and pricing
  @Index()
  @Column({ name: 'currency_id', type: 'uuid' })
  currencyId: string;

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

  // Payment terms integration (TASK-003-01)
  @Index()
  @Column({ name: 'payment_term_id', type: 'uuid', nullable: true })
  paymentTermId: string | null;

  // Sales team
  @Column({ name: 'user_id', type: 'uuid', nullable: true })
  userId: string | null; // Sales representative

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

  // Amounts
  @Column({ name: 'amount_untaxed', type: 'decimal', precision: 15, scale: 2, default: 0 })
  amountUntaxed: number;

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

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

  // Status fields (Order-to-Cash tracking)
  @Index()
  @Column({ type: 'varchar', length: 20, default: 'draft' })
  status: 'draft' | 'sent' | 'sale' | 'done' | 'cancelled';

  @Index()
  @Column({ name: 'invoice_status', type: 'varchar', length: 20, default: 'pending' })
  invoiceStatus: 'pending' | 'partial' | 'invoiced';

  @Index()
  @Column({ name: 'delivery_status', type: 'varchar', length: 20, default: 'pending' })
  deliveryStatus: 'pending' | 'partial' | 'delivered';

  @Column({ name: 'invoice_policy', type: 'varchar', length: 20, default: 'order' })
  invoicePolicy: 'order' | 'delivery';

  // Delivery/Picking integration (TASK-003-03)
  @Column({ name: 'picking_id', type: 'uuid', nullable: true })
  pickingId: string | null;

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

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

  // Confirmation tracking
  @Column({ name: 'confirmed_at', type: 'timestamptz', nullable: true })
  confirmedAt: Date | null;

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

  // Cancellation tracking
  @Column({ name: 'cancelled_at', type: 'timestamptz', nullable: true })
  cancelledAt: Date | null;

  @Column({ name: 'cancelled_by', type: 'uuid', nullable: true })
  cancelledBy: 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;
}
