import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { TaxCategory } from './tax-category.entity';

@Entity({ schema: 'fiscal', name: 'withholding_types' })
@Index('idx_withholding_types_code', ['code'], { unique: true })
@Index('idx_withholding_types_category', ['taxCategoryId'])
export class WithholdingType {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'varchar', length: 20, nullable: false, unique: true })
  code: string;

  @Column({ type: 'varchar', length: 100, nullable: false })
  name: string;

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

  @Column({
    type: 'decimal',
    precision: 5,
    scale: 2,
    nullable: false,
    default: 0,
    name: 'default_rate',
  })
  defaultRate: number;

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

  @ManyToOne(() => TaxCategory, { nullable: true })
  @JoinColumn({ name: 'tax_category_id' })
  taxCategory: TaxCategory | null;

  @Column({ type: 'boolean', default: true, nullable: false, name: 'is_active' })
  isActive: boolean;

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

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