import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Partner } from './partner.entity';

/**
 * Partner Tax Info Entity (schema: partners.partner_tax_info)
 *
 * Extended tax/fiscal information for partners.
 * Stores additional fiscal details required for invoicing and compliance.
 */
@Entity({ name: 'partner_tax_info', schema: 'partners' })
export class PartnerTaxInfo {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'partner_id', type: 'uuid' })
  partnerId: string;

  @ManyToOne(() => Partner, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'partner_id' })
  partner: Partner;

  // Fiscal identification
  @Column({ name: 'tax_id_type', type: 'varchar', length: 20, nullable: true })
  taxIdType: string; // RFC, CURP, EIN, VAT

  @Column({ name: 'tax_id_country', type: 'varchar', length: 3, default: 'MEX' })
  taxIdCountry: string;

  // SAT (Mexico) specific
  @Column({ name: 'sat_regime', type: 'varchar', length: 10, nullable: true })
  satRegime: string; // 601, 603, 612, etc.

  @Column({ name: 'sat_regime_name', type: 'varchar', length: 200, nullable: true })
  satRegimeName: string;

  @Column({ name: 'cfdi_use', type: 'varchar', length: 10, nullable: true })
  cfdiUse: string; // G01, G02, G03, etc.

  @Column({ name: 'cfdi_use_name', type: 'varchar', length: 200, nullable: true })
  cfdiUseName: string;

  @Column({ name: 'fiscal_zip_code', type: 'varchar', length: 10, nullable: true })
  fiscalZipCode: string;

  // Withholding taxes
  @Column({ name: 'withholding_isr', type: 'decimal', precision: 5, scale: 2, default: 0 })
  withholdingIsr: number;

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

  // Validation
  @Column({ name: 'is_verified', type: 'boolean', default: false })
  isVerified: boolean;

  @Column({ name: 'verified_at', type: 'timestamptz', nullable: true })
  verifiedAt: Date;

  @Column({ name: 'verification_source', type: 'varchar', length: 50, nullable: true })
  verificationSource: string; // SAT, MANUAL, API

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

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