import { Entity, PrimaryGeneratedColumn, ManyToOne, JoinColumn, CreateDateColumn, UpdateDateColumn, Column, OneToMany, DeleteDateColumn, Collection, OneToOne } from "typeorm"
import { Campaign } from "./campaign.entity"
import { Users } from "./users.entity"
import { DraftVersions } from "./draft_versions.entity"
import { DraftComments } from "./draft_comments.entity"
import { CampaignInfluencer } from "./campaign_influencer.entity"
import { DisputeHistory } from "./dispute-history.entity"
import { Expose } from "class-transformer"

export enum PaymentMode {
    OFFLINE = "offline",
    ONLINE = "online",
    FORCE_OFFLINE = "force_offline",
}


export enum PAYMENT_STATUS {
    PENDING = "pending",
    COMPLETED = "completed",
    FAILED = "failed"
}

@Entity("campaign_payments")
export class CampaignPayment {
    @PrimaryGeneratedColumn("uuid")
    id: string

    @ManyToOne(() => Users, (influencer) => influencer.id)
    @JoinColumn({ name: "influencer_id" })
    influencer: Users

    @ManyToOne(() => Users, (brand) => brand.id)
    @JoinColumn({ name: "brand_id" })
    brand: Users

    @ManyToOne(() => CampaignInfluencer, (campaignInfluencer) => campaignInfluencer.campaignPayment, { onDelete: "CASCADE" })
    @JoinColumn({ name: "campaign_influencer_id" })
    campaign_influencer: CampaignInfluencer

    @ManyToOne(() => Campaign, (campaign) => campaign.id, { onDelete: "CASCADE" })
    @JoinColumn({ name: "campaign_id" })
    campaign: Campaign

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

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

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

    @Column({ type: 'text', nullable: true })
    invoice_url: string;

    @Column({ type: 'text', nullable: true })
    comment: string;

    @Column({ type: 'text', nullable: true, comment: "INVOICE ADDED BY ADMIN" })
    invoice_receipt: string;

    @Column({ nullable: true })
    currency: string;

    @Column({ type: "decimal", precision: 10, scale: 2, default: 0.00 })
    amount: number

    @Column({ type: "decimal", precision: 10, scale: 2, default: 0.00 })
    amount_usd: number

    @Column({ type: "decimal", precision: 10, scale: 2, default: 0.00, comment: "STRIPE EXCHANGE RATE" })
    exchange_rate: number

    @Column({ type: "decimal", precision: 10, scale: 2, default: 0.00, comment: "STRIPE TRANSACTION FEE" })
    transaction_fee_usd: number

    @Column({ type: "decimal", precision: 10, scale: 2, default: 0.00, comment: "CONNECTOR PLATFORM FEE" })
    platform_fees: number

    @Column({ type: "decimal", precision: 10, scale: 2, default: 0.00, comment: "CONNECTOR PLATFORM FEE USD" })
    platform_fees_usd: number

    @Column({ type: "decimal", precision: 10, scale: 2, default: 0.00, comment: "INFLUENCER PAYABLE AMOUNT" })
    influencer_payable_amount: number

    @Column({ type: "decimal", precision: 10, scale: 2, default: 0.00, comment: "INFLUENCER PAYABLE AMOUNT USD" })
    influencer_payable_amount_usd: number

    @Column({ type: 'boolean', default: false })
    is_admin_paid: boolean;

    @Column({ type: "decimal", precision: 10, scale: 2, default: 0.00, comment: "FINAL AMOUNT IN USD" })
    final_amount_usd: number

    @Column({ type: "enum", enum: PaymentMode, default: PaymentMode.ONLINE })
    payment_mode: PaymentMode

    @Column({ type: "enum", enum: PAYMENT_STATUS, default: PAYMENT_STATUS.PENDING })
    payment_status: PAYMENT_STATUS

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

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

    @Expose()
    campaignCount: string

    @ManyToOne(() => Users, { nullable: true })
    @JoinColumn({ name: 'payed_by' })
    payed_by: Users;

    @CreateDateColumn({
        type: 'timestamp',
        precision: 0,
        default: () => 'CURRENT_TIMESTAMP',
    })
    created_at: Date;

    @UpdateDateColumn({
        type: 'timestamp',
        precision: 0,
        default: () => 'CURRENT_TIMESTAMP',
        onUpdate: 'CURRENT_TIMESTAMP',
    })
    updated_at: Date;

    @DeleteDateColumn({ type: 'timestamp', nullable: true, precision: 0 })
    deleted_at: Date;

}
