import {
    Column,
    CreateDateColumn,
    Entity,
    Index,
    PrimaryGeneratedColumn,
    Unique,
} from "typeorm";

export type WebhookPaymentMethod = "telebirr" | "cbe-birr" | "ebirr";

@Entity({ schema: "freight", name: "payment_webhook_events" })
@Unique(["provider", "externalEventId"])
@Index(["merchantOrderId"])
export class PaymentWebhookEventEntity {
    @PrimaryGeneratedColumn("uuid")
    id!: string;

    @Column({ type: "enum", enum: ["telebirr", "cbe-birr", "ebirr"] })
    provider!: WebhookPaymentMethod;

    @Column({ type: "varchar", length: 255, name: "external_event_id" })
    externalEventId!: string;

    @Column({ type: "varchar", length: 255, nullable: true, name: "merchant_order_id" })
    merchantOrderId?: string;

    @Column({ type: "varchar", length: 255, nullable: true, name: "provider_txn_id" })
    providerTxnId?: string;

    @Column({ type: "boolean", name: "signature_valid" })
    signatureValid!: boolean;

    @Column({ type: "varchar", length: 100 })
    status!: string;

    @Column({ type: "jsonb" })
    payload!: Record<string, unknown>;

    @CreateDateColumn({ name: "received_at" })
    receivedAt!: Date;

    @Column({ type: "timestamp", nullable: true, name: "processed_at" })
    processedAt?: Date;

    @Column({ type: "text", nullable: true, name: "processing_error" })
    processingError?: string;
}
