<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddTimezoneToPreferencesAndSettings extends Migration
{
    public function up()
    {
        // user_preferences.timezone
        if ($this->db->tableExists('user_preferences') && ! $this->db->fieldExists('timezone', 'user_preferences')) {
            $this->forge->addColumn('user_preferences', [
                'timezone' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 64,
                    'null'       => true,
                    'after'      => 'language',
                ],
            ]);
        }

        // settings.timezone (only if table exists)
        if ($this->db->tableExists('settings') && ! $this->db->fieldExists('timezone', 'settings')) {
            $this->forge->addColumn('settings', [
                'timezone' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 64,
                    'null'       => true,
                    'after'      => 'name',
                ],
            ]);
        }
    }

    public function down()
    {
        if ($this->db->tableExists('user_preferences') && $this->db->fieldExists('timezone', 'user_preferences')) {
            $this->forge->dropColumn('user_preferences', 'timezone');
        }

        if ($this->db->tableExists('settings') && $this->db->fieldExists('timezone', 'settings')) {
            $this->forge->dropColumn('settings', 'timezone');
        }
    }
}

