import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
} from 'typeorm';

export type TerminalProvider = 'mercadopago' | 'clip' | 'stripe_terminal';

@Entity({ name: 'tenant_terminal_configs', schema: 'payment_terminals' })
@Index(['tenantId', 'provider', 'name'], { unique: true })
export class TenantTerminalConfig {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({
    type: 'enum',
    enum: ['mercadopago', 'clip', 'stripe_terminal'],
    enumName: 'terminal_provider',
  })
  provider: TerminalProvider;

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

  // Credenciales encriptadas
  @Column({ type: 'jsonb', default: {} })
  credentials: Record<string, any>;

  // Configuración específica del proveedor
  @Column({ type: 'jsonb', default: {} })
  config: Record<string, any>;

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

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

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

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

  // Límites
  @Column({ name: 'daily_limit', type: 'decimal', precision: 12, scale: 2, nullable: true })
  dailyLimit: number | null;

  @Column({ name: 'monthly_limit', type: 'decimal', precision: 12, scale: 2, nullable: true })
  monthlyLimit: number | null;

  @Column({ name: 'transaction_limit', type: 'decimal', precision: 12, scale: 2, nullable: true })
  transactionLimit: number | null;

  // Webhook
  @Column({ name: 'webhook_url', type: 'varchar', length: 500, nullable: true })
  webhookUrl: string | null;

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

  @Column({ type: 'jsonb', default: {} })
  metadata: Record<string, any>;

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

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

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