import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Company } from '../../auth/entities/company.entity.js';

export enum TaxType {
  SALES = 'sales',
  PURCHASE = 'purchase',
  ALL = 'all',
}

@Entity({ schema: 'financial', name: 'taxes' })
@Index('idx_taxes_tenant_id', ['tenantId'])
@Index('idx_taxes_company_id', ['companyId'])
@Index('idx_taxes_code', ['tenantId', 'code'], { unique: true })
@Index('idx_taxes_type', ['taxType'])
export class Tax {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

  @Column({
    type: 'enum',
    enum: TaxType,
    nullable: false,
    name: 'tax_type',
  })
  taxType: TaxType;

  @Column({ type: 'decimal', precision: 5, scale: 2, nullable: false })
  amount: number;

  @Column({ type: 'boolean', default: false, nullable: false, name: 'included_in_price' })
  includedInPrice: boolean;

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

  // Relations
  @ManyToOne(() => Company)
  @JoinColumn({ name: 'company_id' })
  company: Company;

  // Audit fields
  @CreateDateColumn({ name: 'created_at', type: 'timestamp' })
  createdAt: Date;

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

  @UpdateDateColumn({
    name: 'updated_at',
    type: 'timestamp',
    nullable: true,
  })
  updatedAt: Date | null;

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