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

export type PartnerType = 'person' | 'company';

@Entity({ schema: 'core', name: 'partners' })
@Index('idx_partners_tenant_id', ['tenantId'])
@Index('idx_partners_company_id', ['companyId'])
@Index('idx_partners_parent_id', ['parentId'])
@Index('idx_partners_active', ['tenantId', 'active'], { where: 'deleted_at IS NULL' })
@Index('idx_partners_is_customer', ['tenantId', 'isCustomer'], { where: 'deleted_at IS NULL AND is_customer = true' })
@Index('idx_partners_is_supplier', ['tenantId', 'isSupplier'], { where: 'deleted_at IS NULL AND is_supplier = true' })
@Index('idx_partners_is_employee', ['tenantId', 'isEmployee'], { where: 'deleted_at IS NULL AND is_employee = true' })
@Index('idx_partners_tax_id', ['taxId'])
@Index('idx_partners_email', ['email'])
export class Partner {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

  @Column({ type: 'varchar', length: 255, nullable: true, name: 'legal_name' })
  legalName: string | null;

  @Column({
    type: 'varchar',
    length: 20,
    nullable: false,
    default: 'person',
    name: 'partner_type',
  })
  partnerType: PartnerType;

  @Column({ type: 'boolean', default: false, name: 'is_customer' })
  isCustomer: boolean;

  @Column({ type: 'boolean', default: false, name: 'is_supplier' })
  isSupplier: boolean;

  @Column({ type: 'boolean', default: false, name: 'is_employee' })
  isEmployee: boolean;

  @Column({ type: 'boolean', default: false, name: 'is_company' })
  isCompany: boolean;

  @Column({ type: 'varchar', length: 255, nullable: true })
  email: string | null;

  @Column({ type: 'varchar', length: 50, nullable: true })
  phone: string | null;

  @Column({ type: 'varchar', length: 50, nullable: true })
  mobile: string | null;

  @Column({ type: 'varchar', length: 255, nullable: true })
  website: string | null;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'tax_id' })
  taxId: string | null;

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

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

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

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

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

  // Relaciones
  @ManyToOne(() => Tenant, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'tenant_id' })
  tenant: Tenant;

  @ManyToOne(() => Company, { nullable: true })
  @JoinColumn({ name: 'company_id' })
  company: Company | null;

  @ManyToOne(() => Partner, (partner) => partner.children, { nullable: true })
  @JoinColumn({ name: 'parent_id' })
  parentPartner: Partner | null;

  children: Partner[];

  // Auditoría
  @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;

  @Column({ type: 'timestamp', nullable: true, name: 'deleted_at' })
  deletedAt: Date | null;

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

  // Virtual fields for joined data
  companyName?: string;
  currencyCode?: string;
  parentName?: string;
}
