<?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 SQLiteSchemaCompiler 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";

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

        $sql .= " TABLE";

        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 foreign keys
        foreach ($blueprint->foreignKeys as $foreign) {
            $columns[] = $this->compileForeignKey($foreign);
        }

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

        return $sql;
    }

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

        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 .= " AUTOINCREMENT";
        }

        return $sql;
    }

    private function getColumnType(ColumnDefinition $column): string
    {
        return match($column->type) {
            'increments' => 'INTEGER PRIMARY KEY AUTOINCREMENT',
            'bigIncrements' => 'INTEGER PRIMARY KEY AUTOINCREMENT',
            'integer', 'bigInteger', 'tinyInteger', 'smallInteger', 'mediumInteger' => 'INTEGER',
            'float', 'double', 'decimal' => 'REAL',
            'boolean' => 'INTEGER',
            'string' => 'TEXT',
            'text', 'mediumText', 'longText' => 'TEXT',
            'binary' => 'BLOB',
            'json', 'jsonb' => 'TEXT',
            'date', 'dateTime', 'time', 'timestamp' => 'TEXT',
            'enum' => 'TEXT CHECK (`' . $column->name . '` IN (\'' . implode("','", $column->parameters['allowed']) . '\'))',
            default => throw new \InvalidArgumentException("Unknown column type: {$column->type}")
        };
    }

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

        $sql = "FOREIGN KEY ({$localColumns}) 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
    {
        // SQLite has very limited ALTER TABLE support
        // We would need to create a new table and copy data for complex operations
        $statements = [];
        $blueprint = $command->blueprint;

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

        // For drop/rename operations, we'd need table recreation
        foreach ($blueprint->commands as $cmd) {
            if ($cmd instanceof RenameColumnCommand) {
                $statements[] = "ALTER TABLE `{$blueprint->table}` RENAME COLUMN `{$cmd->from}` TO `{$cmd->to}`";
            }
            // Drop column would need table recreation in older SQLite versions
        }

        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 "ALTER TABLE `{$command->from}` RENAME TO `{$command->to}`";
    }

    private function compileDropColumn(DropColumnCommand $command): string
    {
        // SQLite doesn't support DROP COLUMN directly - would need table recreation
        throw new \InvalidArgumentException("SQLite doesn't support dropping columns directly. Table recreation required.");
    }

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

    private function compileDropIndex(DropIndexCommand $command): string
    {
        if (is_array($command->index)) {
            throw new \InvalidArgumentException("SQLite requires index name for dropping");
        }

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

    private function compileDropForeign(DropForeignCommand $command): string
    {
        throw new \InvalidArgumentException("SQLite doesn't support dropping foreign key constraints directly");
    }

    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) . "'";
    }
}
