<?php
/**
 * Migration Schema Builder
 * 
 * @package Licenzo\Framework\Database\Migration
 */

namespace Licenzo\Framework\Database\Migration;

use Licenzo\Framework\Database\Migration\Grammar\GrammarFactory;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Migration Schema Builder for Database Operations
 */
class Schema
{
    /**
     * Create a new table
     *
     * @param string $table
     * @param array $columns
     * @param array $indexes
     * @return Sql
     */
    public static function create($table, array $columns, array $indexes = [])
    {
        $connection = new Connection();
        $grammar = GrammarFactory::make($connection);
        
        return $grammar->compileCreateTable($table, $columns, $indexes);
    }

    /**
     * Drop a table
     *
     * @param string $table
     * @return Sql
     */
    public static function drop($table)
    {
        $connection = new Connection();
        $grammar = GrammarFactory::make($connection);
        
        return $grammar->compileDropTable($table);
    }

    /**
     * Alter an existing table
     *
     * @param string $table
     * @param array $operations
     * @return Sql
     */
    public static function alter($table, array $operations)
    {
        $connection = new Connection();
        $grammar = GrammarFactory::make($connection);
        
        return $grammar->compileAlterTable($table, $operations);
    }

    /**
     * Check if table exists
     *
     * @param string $table
     * @return bool
     */
    public static function hasTable($table)
    {
        $connection = new Connection();
        return $connection->tableExists($table);
    }

    /**
     * Check if column exists in table
     *
     * @param string $table
     * @param string $column
     * @return bool
     */
    public static function hasColumn($table, $column)
    {
        $connection = new Connection();
        $columns = $connection->getColumns($table);
        
        return in_array($column, $columns);
    }

    /**
     * Get table columns
     *
     * @param string $table
     * @return array
     */
    public static function getColumns($table)
    {
        $connection = new Connection();
        return $connection->getColumns($table);
    }

    /**
     * Rename a table
     *
     * @param string $from
     * @param string $to
     * @return Sql
     */
    public static function rename($from, $to)
    {
        $connection = new Connection();
        $grammar = GrammarFactory::make($connection);
        
        return $grammar->compileRenameTable($from, $to);
    }

    /**
     * Truncate a table
     *
     * @param string $table
     * @return Sql
     */
    public static function truncate($table)
    {
        $connection = new Connection();
        $grammar = GrammarFactory::make($connection);
        
        return $grammar->compileTruncateTable($table);
    }

    /**
     * Get WordPress charset and collation
     *
     * @return string
     */
    public static function getCharsetCollate()
    {
        $connection = new Connection();
        return $connection->getCharsetCollate();
    }

    /**
     * Get table prefix
     *
     * @return string
     */
    public static function getPrefix()
    {
        $connection = new Connection();
        return $connection->getPrefix();
    }

    /**
     * Get full table name with prefix
     *
     * @param string $table
     * @return string
     */
    public static function getTableName($table)
    {
        $connection = new Connection();
        return $connection->getPrefix() . $table;
    }

    /**
     * Execute raw SQL for migrations
     *
     * @param string $sql
     * @param array $bindings
     * @return Sql
     */
    public static function raw($sql, array $bindings = [])
    {
        return Sql::raw($sql, ['bindings' => $bindings]);
    }

    /**
     * Create an insert statement
     *
     * @param string $table
     * @param array $data
     * @return Sql
     */
    public static function insert($table, array $data)
    {
        $connection = new Connection();
        $grammar = GrammarFactory::make($connection);
        
        return $grammar->compileInsert($table, $data);
    }

    /**
     * Create an update statement
     *
     * @param string $table
     * @param array $data
     * @param array $where
     * @return Sql
     */
    public static function update($table, array $data, array $where)
    {
        $connection = new Connection();
        $grammar = GrammarFactory::make($connection);
        
        return $grammar->compileUpdate($table, $data, $where);
    }

    /**
     * Create a delete statement
     *
     * @param string $table
     * @param array $where
     * @return Sql
     */
    public static function delete($table, array $where)
    {
        $connection = new Connection();
        $grammar = GrammarFactory::make($connection);
        
        return $grammar->compileDelete($table, $where);
    }
}
