<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    /**
     * Run the migrations.
     * Fix "Data too long for column" - country, genre, language store JSON arrays of IDs.
     */
    public function up(): void
    {
        $driver = DB::getDriverName();
        if ($driver === 'mysql') {
            DB::statement('ALTER TABLE movies MODIFY country TEXT NULL, MODIFY genre TEXT NULL, MODIFY language TEXT NULL');
            DB::statement('ALTER TABLE tv_shows MODIFY country TEXT NULL, MODIFY genre TEXT NULL, MODIFY language TEXT NULL');
        } elseif ($driver === 'pgsql') {
            DB::statement('ALTER TABLE movies ALTER COLUMN country TYPE TEXT, ALTER COLUMN genre TYPE TEXT, ALTER COLUMN language TYPE TEXT');
            DB::statement('ALTER TABLE tv_shows ALTER COLUMN country TYPE TEXT, ALTER COLUMN genre TYPE TEXT, ALTER COLUMN language TYPE TEXT');
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        $driver = DB::getDriverName();
        if ($driver === 'mysql') {
            DB::statement('ALTER TABLE movies MODIFY country VARCHAR(191) NULL, MODIFY genre VARCHAR(191) NULL, MODIFY language VARCHAR(255) NULL');
            DB::statement('ALTER TABLE tv_shows MODIFY country VARCHAR(191) NULL, MODIFY genre VARCHAR(191) NULL, MODIFY language VARCHAR(255) NULL');
        } elseif ($driver === 'pgsql') {
            DB::statement('ALTER TABLE movies ALTER COLUMN country TYPE VARCHAR(191), ALTER COLUMN genre TYPE VARCHAR(191), ALTER COLUMN language TYPE VARCHAR(255)');
            DB::statement('ALTER TABLE tv_shows ALTER COLUMN country TYPE VARCHAR(191), ALTER COLUMN genre TYPE VARCHAR(191), ALTER COLUMN language TYPE VARCHAR(255)');
        }
    }
};
