#!/usr/bin/env php
<?php

use function Termwind\{ask, render};

require 'vendor/autoload.php';

render(<<<'HTML'
    <div>
        <div class="space-y-1">
            <div class="py-1 px-3 bg-amber-600 text-amber-50">
                <strong>Filament v4</strong> Upgrade
            </div>

            <div>
                Welcome to the <strong>Filament v4</strong> upgrade process!

                <br />

                This script will attempt to handle most of the breaking changes for you.

                <br />

                If you have any questions, please reach out to us on <a href="https://filamentphp.com/discord" class="underline font-bold">Discord</a> or <a href="https://github.com/filamentphp/filament/discussions/new?category=q-a" class="underline font-bold">GitHub</a>.
            </div>

            <div>
                To begin, please ensure that you are using a version control system such as Git.

                <br />

                We will make changes directly to your files, and you will need to be able to revert them if something goes wrong.

                <br />

                <strong>Please commit any changes you have made to your project before continuing.</strong>
            </div>
        </div>

        <br />
    </div>
HTML);

$directories = $argv[1] ?? ask(<<<HTML
    <span class="bg-amber-600 text-amber-50 mr-1">
        Please provide a comma-separated list of directories to process breaking changes in (e.g. app, app-modules, src):
    </span>
    HTML) ?: 'app';

render(<<<HTML
    <p class="bg-green-600 text-green-50">
        Starting upgrade...
    </p>
HTML);

$rectorScriptPath = implode(DIRECTORY_SEPARATOR, ['vendor', 'bin', 'rector']);

foreach (explode(',', $directories) as $directory) {
    $directory = trim($directory);

    render(<<<HTML
        <p>
            Start processing <strong>/{$directory}</strong> to fix code affected by breaking changes.
        </p>
        HTML);

    exec("{$rectorScriptPath} process {$directory} --config vendor/filament/upgrade/src/rector.php --clear-cache");

    render(<<<HTML
        <p class="pt-2">
            Finished processing <strong>/{$directory}</strong>.
        </p>
        HTML);
}

$requireCommands = [];

foreach (json_decode(file_get_contents('composer.json'), true)['require'] as $package => $version) {
    if ($package === 'filament/upgrade') {
        continue;
    }

    if (! str_starts_with($package, 'filament/')) {
        continue;
    }

    if ($package === 'filament/spatie-laravel-translatable-plugin') {
        $requireCommands[] = "composer remove filament/spatie-laravel-translatable-plugin -W --no-update";
        $requireCommands[] = "composer require lara-zeus/spatie-translatable -W --no-update";

        continue;
    }

    $isWindows = str_starts_with(php_uname(), 'Windows');

    if (! $isWindows) {
        $requireCommands[] = "composer require {$package}:\"^4.0\" -W --no-update";
    } else {
        $requireCommands[] = "composer require {$package}:\"~4.0\" -W --no-update";
    }
}

$requireCommands = implode("</strong><br /><strong>", $requireCommands);

render(<<<HTML
    <div>
        <span class="bg-green-600 text-green-50">
            Now you're ready to update your Composer dependencies!
        </span>

        <br /> <br />

        First require new versions of Filament packages:

        <br />

        <strong>{$requireCommands}</strong>

        <br /> <br />

        If you have any third party plugins that need to be upgraded, you should bump those dependencies as well.

        <br /> <br />

        And then run:

        <br />

        <strong>composer update</strong>

        <br /> <br />

        If you have any questions, please reach out to us on <a href="https://filamentphp.com/discord" class="underline font-bold">Discord</a> or <a href="https://github.com/filamentphp/filament/discussions/new?category=q-a" class="underline font-bold">GitHub</a>.
    </div>
HTML);
