<?php

declare(strict_types=1);

namespace App\Framework\Database\Schema;

use App\Framework\Database\Schema\Commands\{
    AlterTableCommand,
    CreateTableCommand,
    DropColumnCommand,
    DropForeignCommand,
    DropIndexCommand,
    DropTableCommand,
    RawCommand,
    RenameColumnCommand,
    RenameTableCommand
};

final class MySQLSchemaCompiler implements SchemaCompiler
{
    public function compile(object $command): string|array
    {
        return match($command::class) {
            CreateTableCommand::class => $this->compileCreateTable($command),
            AlterTableCommand::class => $this->compileAlterTable($command),
            DropTableCommand::class => $this->compileDropTable($command),
            RenameTableCommand::class => $this->compileRenameTable($command),
            DropColumnCommand::class => $this->compileDropColumn($command),
            RenameColumnCommand::class => $this->compileRenameColumn($command),
            DropIndexCommand::class => $this->compileDropIndex($command),
            DropForeignCommand::class => $this->compileDropForeign($command),
            RawCommand::class => $command->sql,
            default => throw new \InvalidArgumentException('Unknown command: ' . $command::class)
        };
    }

    private function compileCreateTable(CreateTableCommand $command): string
    {
        $blueprint = $command->blueprint;
        $sql = "CREATE TABLE";

        if ($blueprint->temporary) {
            $sql .= " TEMPORARY";
        }

        if ($blueprint->ifNotExists) {
            $sql .= " IF NOT EXISTS";
        }

        $sql .= " `{$blueprint->table}` (";

        // Columns
        $columns = [];
        foreach ($blueprint->columns as $column) {
            $columns[] = $this->compileColumn($column);
        }

        // Add primary key from columns
        $primaryColumns = [];
        foreach ($blueprint->columns as $column) {
            if ($column->primary) {
                $primaryColumns[] = "`{$column->name}`";
            }
        }

        if ($primaryColumns) {
            $columns[] = "PRIMARY KEY (" . implode(', ', $primaryColumns) . ")";
        }

        // Add indexes
        foreach ($blueprint->indexes as $index) {
            $columns[] = $this->compileIndex($index);
        }

        // Add foreign keys
        foreach ($blueprint->foreignKeys as $foreign) {
            $columns[] = $this->compileForeignKey($foreign);
        }

        $sql .= implode(', ', $columns) . ")";

        // Table options
        if ($blueprint->engine) {
            $sql .= " ENGINE={$blueprint->engine}";
        }

        if ($blueprint->charset) {
            $sql .= " DEFAULT CHARSET={$blueprint->charset}";
        }

        if ($blueprint->collation) {
            $sql .= " COLLATE={$blueprint->collation}";
        }

        return $sql;
    }

    private function compileColumn(ColumnDefinition $column): string
    {
        $sql = "`{$column->name}` " . $this->getColumnType($column);

        if ($column->unsigned) {
            $sql .= " UNSIGNED";
        }

        if (! $column->nullable) {
            $sql .= " NOT NULL";
        }

        if ($column->hasDefault) {
            if ($column->default === 'CURRENT_TIMESTAMP') {
                $sql .= " DEFAULT CURRENT_TIMESTAMP";
            } else {
                $sql .= " DEFAULT " . $this->quoteValue($column->default);
            }
        }

        if ($column->autoIncrement) {
            $sql .= " AUTO_INCREMENT";
        }

        if ($column->comment) {
            $sql .= " COMMENT " . $this->quoteValue($column->comment);
        }

        return $sql;
    }

    private function getColumnType(ColumnDefinition $column): string
    {
        return match($column->type) {
            'increments' => 'INT',
            'bigIncrements' => 'BIGINT',
            'integer' => 'INT',
            'bigInteger' => 'BIGINT',
            'tinyInteger' => 'TINYINT',
            'smallInteger' => 'SMALLINT',
            'mediumInteger' => 'MEDIUMINT',
            'float' => sprintf('FLOAT(%d,%d)', $column->parameters['precision'] ?? 8, $column->parameters['scale'] ?? 2),
            'double' => $column->parameters['precision'] ? sprintf('DOUBLE(%d,%d)', $column->parameters['precision'], $column->parameters['scale'] ?? 2) : 'DOUBLE',
            'decimal' => sprintf('DECIMAL(%d,%d)', $column->parameters['precision'] ?? 8, $column->parameters['scale'] ?? 2),
            'boolean' => 'TINYINT(1)',
            'string' => sprintf('VARCHAR(%d)', $column->parameters['length'] ?? 255),
            'text' => 'TEXT',
            'mediumText' => 'MEDIUMTEXT',
            'longText' => 'LONGTEXT',
            'binary' => 'BLOB',
            'json' => 'JSON',
            'date' => 'DATE',
            'dateTime' => $column->parameters['precision'] > 0 ? sprintf('DATETIME(%d)', $column->parameters['precision']) : 'DATETIME',
            'time' => $column->parameters['precision'] > 0 ? sprintf('TIME(%d)', $column->parameters['precision']) : 'TIME',
            'timestamp' => $column->parameters['precision'] > 0 ? sprintf('TIMESTAMP(%d)', $column->parameters['precision']) : 'TIMESTAMP',
            'enum' => "ENUM('" . implode("','", $column->parameters['allowed']) . "')",
            default => throw new \InvalidArgumentException("Unknown column type: {$column->type}")
        };
    }

    private function compileIndex(IndexDefinition $index): string
    {
        $columns = '`' . implode('`, `', $index->columns) . '`';

        return match($index->type) {
            IndexType::UNIQUE => $index->name ? "UNIQUE KEY `{$index->name}` ({$columns})" : "UNIQUE ({$columns})",
            IndexType::INDEX => $index->name ? "KEY `{$index->name}` ({$columns})" : "KEY ({$columns})",
            IndexType::FULLTEXT => $index->name ? "FULLTEXT KEY `{$index->name}` ({$columns})" : "FULLTEXT ({$columns})",
            IndexType::SPATIAL => $index->name ? "SPATIAL KEY `{$index->name}` ({$columns})" : "SPATIAL ({$columns})",
            default => throw new \InvalidArgumentException("Unsupported index type for MySQL: {$index->type->value}")
        };
    }

    private function compileForeignKey(ForeignKeyDefinition $foreign): string
    {
        $localColumns = '`' . implode('`, `', $foreign->columns) . '`';
        $foreignColumns = '`' . implode('`, `', $foreign->referencedColumns) . '`';

        $sql = $foreign->name
            ? "CONSTRAINT `{$foreign->name}` FOREIGN KEY ({$localColumns})"
            : "FOREIGN KEY ({$localColumns})";

        $sql .= " REFERENCES `{$foreign->referencedTable}` ({$foreignColumns})";

        if ($foreign->onUpdate !== ForeignKeyAction::RESTRICT) {
            $sql .= " ON UPDATE {$foreign->onUpdate->value}";
        }

        if ($foreign->onDelete !== ForeignKeyAction::RESTRICT) {
            $sql .= " ON DELETE {$foreign->onDelete->value}";
        }

        return $sql;
    }

    private function compileAlterTable(AlterTableCommand $command): array
    {
        $statements = [];
        $blueprint = $command->blueprint;

        // Add columns
        foreach ($blueprint->columns as $column) {
            $sql = "ALTER TABLE `{$blueprint->table}` ADD COLUMN " . $this->compileColumn($column);

            if ($column->after) {
                $sql .= " AFTER `{$column->after}`";
            } elseif ($column->first) {
                $sql .= " FIRST";
            }

            $statements[] = $sql;
        }

        // Process commands
        foreach ($blueprint->commands as $cmd) {
            $statements[] = $this->compile($cmd);
        }

        return $statements;
    }

    private function compileDropTable(DropTableCommand $command): string
    {
        return $command->ifExists
            ? "DROP TABLE IF EXISTS `{$command->table}`"
            : "DROP TABLE `{$command->table}`";
    }

    private function compileRenameTable(RenameTableCommand $command): string
    {
        return "RENAME TABLE `{$command->from}` TO `{$command->to}`";
    }

    private function compileDropColumn(DropColumnCommand $command): string
    {
        $columns = array_map(fn ($col) => "`{$col}`", $command->columns);

        return "DROP COLUMN " . implode(', DROP COLUMN ', $columns);
    }

    private function compileRenameColumn(RenameColumnCommand $command): string
    {
        return "CHANGE COLUMN `{$command->from}` `{$command->to}`";
    }

    private function compileDropIndex(DropIndexCommand $command): string
    {
        if (is_array($command->index)) {
            $columns = '`' . implode('`, `', $command->index) . '`';

            return "DROP INDEX ({$columns})";
        }

        return "DROP INDEX `{$command->index}`";
    }

    private function compileDropForeign(DropForeignCommand $command): string
    {
        if (is_array($command->index)) {
            throw new \InvalidArgumentException("MySQL requires foreign key constraint name for dropping");
        }

        return "DROP FOREIGN KEY `{$command->index}`";
    }

    private function quoteValue(mixed $value): string
    {
        if ($value === null) {
            return 'NULL';
        }

        if (is_bool($value)) {
            return $value ? '1' : '0';
        }

        if (is_numeric($value)) {
            return (string) $value;
        }

        return "'" . str_replace("'", "''", (string) $value) . "'";
    }
}
