/**
 * PaymentTransaction Entity
 * Mobile payment processing and tracking
 * Compatible with erp-core payment-transaction.entity
 *
 * @module Mobile
 */

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

// erp-core base + construction-specific POS values
export type PaymentSourceType = 'sale' | 'invoice' | 'subscription' | 'pos' | 'mobile' | 'web' | 'api';
export type PaymentMethod = 'card' | 'contactless' | 'qr' | 'link' | 'cash' | 'transfer';
export type PaymentStatus = 'pending' | 'processing' | 'completed' | 'failed' | 'refunded' | 'cancelled';
export type CardType = 'credit' | 'debit';

@Entity({ name: 'payment_transactions', schema: 'mobile' })
export class PaymentTransaction {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Index()
  @Column({ name: 'branch_id', type: 'uuid', nullable: true })
  branchId: string;

  @Index()
  @Column({ name: 'user_id', type: 'uuid' })
  userId: string;

  @Column({ name: 'device_id', type: 'uuid', nullable: true })
  deviceId: string;

  @Index()
  @Column({ name: 'source_type', type: 'varchar', length: 30 })
  sourceType: PaymentSourceType;

  @Column({ name: 'source_id', type: 'uuid' })
  sourceId: string;

  @Column({ name: 'terminal_provider', type: 'varchar', length: 30 })
  terminalProvider: string;

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

  @Index()
  @Column({ name: 'external_transaction_id', type: 'varchar', length: 255, nullable: true })
  externalTransactionId: string;

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

  @Column({ type: 'varchar', length: 3, default: 'MXN' })
  currency: string;

  @Column({ name: 'tip_amount', type: 'decimal', precision: 12, scale: 2, default: 0 })
  tipAmount: number;

  @Column({ name: 'total_amount', type: 'decimal', precision: 12, scale: 2 })
  totalAmount: number;

  @Column({ name: 'payment_method', type: 'varchar', length: 30 })
  paymentMethod: PaymentMethod;

  @Column({ name: 'card_brand', type: 'varchar', length: 20, nullable: true })
  cardBrand: string;

  @Column({ name: 'card_last_four', type: 'varchar', length: 4, nullable: true })
  cardLastFour: string;

  @Column({ name: 'card_type', type: 'varchar', length: 20, nullable: true })
  cardType: CardType;

  @Index()
  @Column({ type: 'varchar', length: 20, default: 'pending' })
  status: PaymentStatus;

  @Column({ name: 'failure_reason', type: 'text', nullable: true })
  failureReason: string;

  @Column({ name: 'initiated_at', type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP' })
  initiatedAt: Date;

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

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

  @Column({ name: 'receipt_url', type: 'text', nullable: true })
  receiptUrl: string;

  @Column({ name: 'receipt_sent', type: 'boolean', default: false })
  receiptSent: boolean;

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

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

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