<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateCreditsAndWallet extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id'           => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'action'       => ['type' => 'ENUM', 'constraint' => ['contact_reveal', 'resume_download', 'candidate_unlock', 'messaging']],
            'credit_cost'  => ['type' => 'INT', 'unsigned' => true, 'default' => 1],
            'updated_at'   => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('action');
        $this->forge->createTable('credit_costs', true, ['ENGINE' => 'InnoDB']);

        $this->forge->addField([
            'id'            => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'agency_id'     => ['type' => 'BIGINT', 'unsigned' => true],
            'balance'       => ['type' => 'INT', 'default' => 0],
            'last_updated'  => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('agency_id');
        $this->forge->addForeignKey('agency_id', 'agencies', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('credit_balances', true, ['ENGINE' => 'InnoDB']);

        $this->forge->addField([
            'id'              => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'agency_id'       => ['type' => 'BIGINT', 'unsigned' => true],
            'user_id'         => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'type'            => ['type' => 'ENUM', 'constraint' => ['purchase', 'usage', 'refund', 'expiry', 'bonus']],
            'amount'          => ['type' => 'INT'],
            'balance_after'   => ['type' => 'INT'],
            'reference_type'  => ['type' => 'VARCHAR', 'constraint' => 50, 'null' => true],
            'reference_id'    => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'expires_at'      => ['type' => 'DATE', 'null' => true],
            'created_at'      => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey(['agency_id', 'created_at']);
        $this->forge->addForeignKey('agency_id', 'agencies', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('credit_transactions', true, ['ENGINE' => 'InnoDB']);

        $this->forge->addField([
            'id'          => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'agency_id'   => ['type' => 'BIGINT', 'unsigned' => true],
            'balance'     => ['type' => 'DECIMAL', 'constraint' => '12,2', 'default' => 0],
            'currency'    => ['type' => 'VARCHAR', 'constraint' => 10, 'default' => 'PKR'],
            'updated_at'  => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('agency_id');
        $this->forge->addForeignKey('agency_id', 'agencies', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('wallets', true, ['ENGINE' => 'InnoDB']);

        $this->forge->addField([
            'id'              => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'wallet_id'       => ['type' => 'BIGINT', 'unsigned' => true],
            'type'            => ['type' => 'ENUM', 'constraint' => ['deposit', 'withdrawal', 'credit_purchase', 'refund', 'adjustment']],
            'amount'          => ['type' => 'DECIMAL', 'constraint' => '12,2'],
            'balance_after'   => ['type' => 'DECIMAL', 'constraint' => '12,2'],
            'reference_type'  => ['type' => 'VARCHAR', 'constraint' => 50, 'null' => true],
            'reference_id'    => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'note'            => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
            'created_at'      => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('wallet_id', 'wallets', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('wallet_transactions', true, ['ENGINE' => 'InnoDB']);
    }

    public function down(): void
    {
        $this->forge->dropTable('wallet_transactions', true);
        $this->forge->dropTable('wallets', true);
        $this->forge->dropTable('credit_transactions', true);
        $this->forge->dropTable('credit_balances', true);
        $this->forge->dropTable('credit_costs', true);
    }
}
