<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddRewardExpiryFields extends Migration
{
    public function up()
    {
        if (!$this->db->fieldExists('reward_points_expire_days', 'rewarder_reward_rules')) {
            $this->forge->addColumn('rewarder_reward_rules', [
                'reward_points_expire_days' => [
                    'type' => 'INT',
                    'constraint' => 11,
                    'null' => false,
                    'default' => 0,
                    'after' => 'value',
                ],
            ]);
        }

        if (!$this->db->fieldExists('status', 'rewarder_ledger')) {
            $this->forge->addColumn('rewarder_ledger', [
                'status' => [
                    'type' => 'TINYINT',
                    'constraint' => 1,
                    'null' => false,
                    'default' => 1,
                    'after' => 'description',
                ],
            ]);
        }

        if (!$this->db->fieldExists('expiry_date', 'rewarder_ledger')) {
            $this->forge->addColumn('rewarder_ledger', [
                'expiry_date' => [
                    'type' => 'DATE',
                    'null' => true,
                    'after' => 'status',
                ],
            ]);
        }
    }

    public function down()
    {
        if ($this->db->fieldExists('expiry_date', 'rewarder_ledger')) {
            $this->forge->dropColumn('rewarder_ledger', 'expiry_date');
        }
        if ($this->db->fieldExists('status', 'rewarder_ledger')) {
            $this->forge->dropColumn('rewarder_ledger', 'status');
        }
        if ($this->db->fieldExists('reward_points_expire_days', 'rewarder_reward_rules')) {
            $this->forge->dropColumn('rewarder_reward_rules', 'reward_points_expire_days');
        }
    }
}

