// Entity representing the external ERP products table (maeplu) - READ ONLY.
//
// CRITICO: pagaiva alimenta facturacion del cliente. Cualquier escritura
// accidental sobre esta tabla (upsert descuidado, cascada, preload) corrompe
// datos de produccion con consecuencia directa en facturacion. Por eso cada
// columna tiene insert:false/update:false — TypeORM nunca las incluye en un
// INSERT/UPDATE generado por save()/insert()/update(entity), sin depender de
// que nadie recuerde no llamarlos. Ademas, el unico repositorio que debe
// inyectarse para esta entidad es ReadonlyRepository<Producto> (ver
// productos.module.ts), que no expone save/insert/update/delete/remove.
import { Column, Entity, Index, PrimaryColumn } from "typeorm";
import { charBooleanTransformer } from "../../common/transformers/char-boolean.transformer";

const READONLY = { insert: false, update: false } as const;

@Entity("maeplu")
export class Producto {
  @PrimaryColumn({ length: 24 })
  plu: string;

  @Column({ length: 255, nullable: true, ...READONLY })
  desclarga: string;

  @Column({ length: 100, nullable: true, ...READONLY })
  desccorta: string;

  @Column({ length: 50, nullable: true, ...READONLY })
  @Index()
  alterno: string;

  @Column({ type: "decimal", precision: 12, scale: 2, default: 0, ...READONLY })
  precio: number;

  // Stored as char(1) 'S'/'N' in maeplu, not tinyint — see charBooleanTransformer.
  // pagaiva especificamente alimenta facturacion — nunca se escribe desde aca.
  @Column({
    type: "char",
    length: 1,
    nullable: true,
    transformer: charBooleanTransformer,
    ...READONLY,
  })
  pagaiva: boolean;

  @Column({ type: "decimal", precision: 5, scale: 2, default: 0, ...READONLY })
  iva: number;

  @Column({
    name: "es_servicio",
    type: "char",
    length: 1,
    nullable: true,
    transformer: charBooleanTransformer,
    ...READONLY,
  })
  es_servicio: boolean;

  @Column({
    name: "usainventario",
    type: "char",
    length: 1,
    nullable: true,
    transformer: charBooleanTransformer,
    ...READONLY,
  })
  usainventario: boolean;

  // Not part of the base ERP schema (absent from the master template
  // mbinvmacrobase) — added by hand on client databases that use the taller
  // module. The onboarding migration adds these additively if missing.
  @Column({
    name: "es_taller",
    type: "char",
    length: 1,
    nullable: true,
    transformer: charBooleanTransformer,
    ...READONLY,
  })
  es_taller: boolean;

  @Column({
    name: "es_producto",
    type: "char",
    length: 1,
    nullable: true,
    transformer: charBooleanTransformer,
    ...READONLY,
  })
  es_producto: boolean;
}
