<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddPrizeColumnsToCompetitionClassWinners extends Migration
{
    public function up()
    {
        $db = \Config\Database::connect();
        if (!$db->tableExists('competition_class_winners')) {
            return;
        }

        $columns = [
            'prize_1' => ['type' => 'DECIMAL', 'constraint' => '10,2', 'null' => true],
            'prize_2' => ['type' => 'DECIMAL', 'constraint' => '10,2', 'null' => true],
            'prize_3' => ['type' => 'DECIMAL', 'constraint' => '10,2', 'null' => true],
            'prize_4' => ['type' => 'DECIMAL', 'constraint' => '10,2', 'null' => true],
            'prize_5' => ['type' => 'DECIMAL', 'constraint' => '10,2', 'null' => true],
            'prize_6' => ['type' => 'DECIMAL', 'constraint' => '10,2', 'null' => true],
        ];

        $add = [];
        foreach ($columns as $name => $def) {
            if (!$db->fieldExists($name, 'competition_class_winners')) {
                $add[$name] = $def;
            }
        }

        if (!empty($add)) {
            $this->forge->addColumn('competition_class_winners', $add);
        }
    }

    public function down()
    {
        $db = \Config\Database::connect();
        if (!$db->tableExists('competition_class_winners')) {
            return;
        }

        $names = ['prize_1','prize_2','prize_3','prize_4','prize_5','prize_6'];
        foreach ($names as $name) {
            if ($db->fieldExists($name, 'competition_class_winners')) {
                $this->forge->dropColumn('competition_class_winners', $name);
            }
        }
    }
}
