<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddStyleToPreferences extends Migration
{
    public function up()
    {
        if ($this->db->tableExists('user_preferences')) {
            if (! $this->db->fieldExists('style_color', 'user_preferences')) {
                $styleColor = [
                    'type'       => 'VARCHAR',
                    'constraint' => 32,
                    'null'       => true,
                ];

                if ($this->db->fieldExists('timezone', 'user_preferences')) {
                    $styleColor['after'] = 'timezone';
                }

                $this->forge->addColumn('user_preferences', [
                    'style_color' => $styleColor,
                ]);
            }
            if (! $this->db->fieldExists('menu_color', 'user_preferences')) {
                $this->forge->addColumn('user_preferences', [
                    'menu_color' => [
                        'type'       => 'VARCHAR',
                        'constraint' => 32,
                        'null'       => true,
                        'after'      => 'style_color',
                    ],
                ]);
            }
            if (! $this->db->fieldExists('menu_custom_bg', 'user_preferences')) {
                $this->forge->addColumn('user_preferences', [
                    'menu_custom_bg' => [
                        'type'       => 'VARCHAR',
                        'constraint' => 16,
                        'null'       => true,
                        'after'      => 'menu_color',
                    ],
                ]);
            }
            if (! $this->db->fieldExists('menu_custom_text', 'user_preferences')) {
                $this->forge->addColumn('user_preferences', [
                    'menu_custom_text' => [
                        'type'       => 'VARCHAR',
                        'constraint' => 16,
                        'null'       => true,
                        'after'      => 'menu_custom_bg',
                    ],
                ]);
            }
            if (! $this->db->fieldExists('menu_custom_mode', 'user_preferences')) {
                $this->forge->addColumn('user_preferences', [
                    'menu_custom_mode' => [
                        'type'       => 'VARCHAR',
                        'constraint' => 8,
                        'null'       => true,
                        'after'      => 'menu_custom_text',
                    ],
                ]);
            }
        }
    }

    public function down()
    {
        if ($this->db->tableExists('user_preferences')) {
            if ($this->db->fieldExists('menu_custom_mode', 'user_preferences')) {
                $this->forge->dropColumn('user_preferences', 'menu_custom_mode');
            }
            if ($this->db->fieldExists('menu_custom_text', 'user_preferences')) {
                $this->forge->dropColumn('user_preferences', 'menu_custom_text');
            }
            if ($this->db->fieldExists('menu_custom_bg', 'user_preferences')) {
                $this->forge->dropColumn('user_preferences', 'menu_custom_bg');
            }
            if ($this->db->fieldExists('menu_color', 'user_preferences')) {
                $this->forge->dropColumn('user_preferences', 'menu_color');
            }
            if ($this->db->fieldExists('style_color', 'user_preferences')) {
                $this->forge->dropColumn('user_preferences', 'style_color');
            }
        }
    }
}
