<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddFootballFixtureCacheFields extends Migration
{
    public function up()
    {
        if (!$this->db->fieldExists('last_fixtures_sync_at', 'football_settings')) {
            $this->forge->addColumn('football_settings', [
                'last_fixtures_sync_at' => [
                    'type' => 'DATETIME',
                    'null' => true,
                    'after' => 'last_reference_sync_at',
                ],
                'fixtures_cache_from' => [
                    'type' => 'DATE',
                    'null' => true,
                    'after' => 'last_fixtures_sync_at',
                ],
                'fixtures_cache_to' => [
                    'type' => 'DATE',
                    'null' => true,
                    'after' => 'fixtures_cache_from',
                ],
            ]);
        }
    }

    public function down()
    {
        if ($this->db->fieldExists('fixtures_cache_to', 'football_settings')) {
            $this->forge->dropColumn('football_settings', 'fixtures_cache_to');
        }
        if ($this->db->fieldExists('fixtures_cache_from', 'football_settings')) {
            $this->forge->dropColumn('football_settings', 'fixtures_cache_from');
        }
        if ($this->db->fieldExists('last_fixtures_sync_at', 'football_settings')) {
            $this->forge->dropColumn('football_settings', 'last_fixtures_sync_at');
        }
    }
}
