import { Entity, Column, ManyToOne, JoinColumn, Index } from 'typeorm';
import { BaseEntity } from '../database/base.entity';
import { TaxTypeEntity } from './tax-type.entity';

/**
 * TaxComponent — a named sub-component under a TaxType (structure
 * only, no rate). Per stakeholder spec the tax TYPE master owns the
 * shape — "GST consists of CGST and SGST" — and individual
 * destinations fill in the actual percentages for each component on
 * their tax slab.
 *
 * Examples:
 *   TaxType "GST" → components: CGST, SGST, IGST
 *   TaxType "VAT" → components: VAT (single)
 *   TaxType "Tourism Tax" → components: Tourism Tax
 *
 * Kept flexible: a tenant with a simple flat tax just registers one
 * component; a tenant in India registers the three standard GST
 * components. Rates live on `tax_slabs.component_rates` (jsonb
 * keyed by component_id).
 */
@Entity('tax_components')
@Index(['tenant_id', 'tax_type_id', 'name'], { unique: true, where: '"is_deleted" = false' })
export class TaxComponentEntity extends BaseEntity {
  @ManyToOne(() => TaxTypeEntity, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'tax_type_id' })
  tax_type: TaxTypeEntity;

  @Column({ type: 'uuid' })
  tax_type_id: string;

  /** e.g. "CGST" / "SGST" / "IGST" / "Cess" / "VAT". Free-form. */
  @Column({ type: 'varchar', length: 100 })
  name: string;

  @Column({ type: 'int', default: 0 })
  sort_order: number;

  @Column({ type: 'boolean', default: true })
  is_active: boolean;
}
