import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  OneToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Branch } from './branch.entity';

/**
 * Configuración de inventario por sucursal.
 * Mapea a core.branch_inventory_settings (DDL: 03-core-branches.sql)
 */
@Entity({ name: 'branch_inventory_settings', schema: 'core' })
export class BranchInventorySettings {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'branch_id', type: 'uuid' })
  branchId: string;

  @OneToOne(() => Branch, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'branch_id' })
  branch: Branch;

  // Almacén asociado (referencia externa a inventory.warehouses)
  @Column({ name: 'warehouse_id', type: 'uuid', nullable: true })
  warehouseId: string;

  // Configuración de stock
  @Column({ name: 'default_stock_min', type: 'integer', default: 0 })
  defaultStockMin: number;

  @Column({ name: 'default_stock_max', type: 'integer', default: 1000 })
  defaultStockMax: number;

  @Column({ name: 'auto_reorder_enabled', type: 'boolean', default: false })
  autoReorderEnabled: boolean;

  // Configuración de precios (referencia externa a sales.price_lists)
  @Column({ name: 'price_list_id', type: 'uuid', nullable: true })
  priceListId: string;

  @Column({ name: 'allow_price_override', type: 'boolean', default: false })
  allowPriceOverride: boolean;

  @Column({ name: 'max_discount_percent', type: 'decimal', precision: 5, scale: 2, default: 0 })
  maxDiscountPercent: number;

  // Configuración de impuestos
  @Column({ name: 'tax_config', type: 'jsonb', default: {} })
  taxConfig: Record<string, any>;

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

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