import { MigrationInterface, QueryRunner } from "typeorm";

export class CreatePaymentTable1780639311366 implements MigrationInterface {
    name = "CreatePaymentTable1780639311366";

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            CREATE TYPE freight.payments_type_enum AS ENUM ('booking');
        `);

        await queryRunner.query(`
            CREATE TYPE freight.payments_method_enum AS ENUM ('telebirr', 'cbe-birr', 'ebirr');
        `);

        await queryRunner.query(`
            CREATE TYPE freight.payments_currency_enum AS ENUM ('ETB', 'USD');
        `);

        await queryRunner.query(`
            CREATE TYPE freight.payments_status_enum AS ENUM (
                'action-required',
                'processing',
                'success',
                'failed',
                'canceled',
                'refunded'
            );
        `);

        await queryRunner.query(`
    CREATE TABLE freight.payments (
        id uuid NOT NULL DEFAULT uuid_generate_v4(),

        ref_id varchar(255) NOT NULL,

        type freight.payments_type_enum NOT NULL,

        method freight.payments_method_enum NOT NULL,

        currency freight.payments_currency_enum NOT NULL,

        amount numeric NOT NULL,

        raw_initiation jsonb NOT NULL DEFAULT '{}'::jsonb,

        client_action json,

        merchant_order_id varchar(255) NOT NULL,

        transaction_id varchar(255),

        status freight.payments_status_enum NOT NULL DEFAULT 'action-required',

        paid_at date,

        refunded_at date,

        expires_at date,

        failer_code varchar(30),

        failer_message varchar(255),

        reason varchar(255),

        created_at TIMESTAMP NOT NULL DEFAULT now(),

        CONSTRAINT PK_payments PRIMARY KEY (id),

        CONSTRAINT UQ_payments_merchant_order_id UNIQUE (merchant_order_id),

        CONSTRAINT UQ_payments_transaction_id UNIQUE (transaction_id)
    );
`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            DROP TABLE IF EXISTS freight.payments;
        `);

        await queryRunner.query(`
            DROP TYPE IF EXISTS freight.payments_status_enum;
        `);

        await queryRunner.query(`
            DROP TYPE IF EXISTS freight.payments_currency_enum;
        `);

        await queryRunner.query(`
            DROP TYPE IF EXISTS freight.payments_method_enum;
        `);

        await queryRunner.query(`
            DROP TYPE IF EXISTS freight.payments_type_enum;
        `);
    }
}