<?php

declare(strict_types=1);

namespace App\Framework\Database\Migration\Commands;

use App\Framework\Console\ConsoleCommand;
use App\Framework\Console\ExitCode;
use App\Framework\Console\Parameter;
use App\Framework\Database\Migration\MigrationGenerator;

final readonly class MakeMigrationCommand
{
    public function __construct(
        private MigrationGenerator $generator
    ) {
    }

    /**
     * Generate a new migration file
     *
     * @param string $name Migration name (e.g. CreateUsersTable)
     * @param string $domain Domain name for the migration
     */
    #[ConsoleCommand('make:migration', 'Generate a new migration file')]
    public function __invoke(
        #[Parameter('Migration name (e.g. CreateUsersTable)', example: 'CreateUsersTable')]
        string $name,
        #[Parameter('Domain name for organizing migrations')]
        string $domain = 'Media'
    ): ExitCode {
        if (empty(trim($name))) {
            echo "Error: Migration name cannot be empty\n";

            return ExitCode::INVALID_INPUT;
        }

        try {
            $filePath = $this->generator->generate($name, $domain);
            echo "✓ Migration created: {$filePath}\n";

            return ExitCode::SUCCESS;
        } catch (\Exception $e) {
            echo "✗ Error creating migration: {$e->getMessage()}\n";

            return ExitCode::GENERAL_ERROR;
        }
    }
}
