import { BaseEntity, Column, CreateDateColumn, Entity, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { PaymentRefundEntity } from "./payment-refund.entity";


type PaymentType = "booking"
type PaymentMethod = "telebirr" | "cbe-birr" | "ebirr" | "waafi" | "card" | "dmoney" | "cac-bank"
type Currency = "ETB" | "USD"
export type PaymentStatus = "action-required" | "processing" | "success" | "failed" | "canceled" | "refunded"

@Entity({ schema: 'freight', name: 'payments' })
export class PaymentEntity extends BaseEntity {
    @PrimaryGeneratedColumn("uuid")
    id!: string

    @Column({ type: 'varchar', length: 255, name: "ref_id" })
    refId!: string

    @Column({ type: "enum", enum: ["booking"] })
    type!: PaymentType;

    @Column({ type: "enum", enum: ["telebirr", "cbe-birr", "ebirr", "waafi", "card", "dmoney", "cac-bank"] })
    method!: PaymentMethod

    @Column({ type: "enum", enum: ["ETB", "USD"] })
    currency!: Currency

    @Column({ type: "numeric" })
    amount!: number

    @Column({ type: "varchar", length: 255, name: "reason", })
    reason?: string;

    @Column({ type: "jsonb", default: {}, name: "raw_initiation" })
    rawInitiation?: Record<string, unknown>

    @Column({ type: "jsonb", nullable: true, name: "client_action" })
    clientAction?: Record<string, unknown>;

    @Column({ type: "varchar", length: 255, unique: true, name: "merchant_order_id", })
    merchantOrderId!: string

    @Column({ type: "varchar", length: 255, unique: true, nullable: true, name: "transaction_id", })
    transactionId?: string

    @Column({ type: "enum", enum: ["action-required", "processing", "success", "failed", "canceled", "refunded"], default: "action-required" })
    status!: PaymentStatus

    @Column({ type: "date", nullable: true, name: "paid_at" })
    paidAt?: Date

    @Column({ type: "timestamp", nullable: true, name: "refunded_at" })
    refundedAt?: Date

    @Column({ type: "timestamp", nullable: true, name: "expires_at" })
    expiresAt?: Date

    @Column({ type: "varchar", length: 30, nullable: true, name: "failer_code" })
    failerCode?: string

    @Column({ type: "varchar", length: 255, nullable: true, name: "failer_message" })
    failureMessage?: string

    @CreateDateColumn({ name: "created_at" })
    createdAt!: Date

    @OneToMany(() => PaymentRefundEntity, (refund) => refund.payment)
    refunds!: PaymentRefundEntity[];

}