<?php
/**
 * Base Migration Class
 * 
 * @package Licenzo\Framework\Database\Migration
 */

namespace Licenzo\Framework\Database\Migration;

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

/**
 * Abstract Base Migration Class
 */
abstract class Migration
{
    /**
     * Migration timestamp
     *
     * @var string
     */
    protected $timestamp;

    /**
     * Migration name
     *
     * @var string
     */
    protected $name;

    /**
     * Create a new migration instance
     */
    public function __construct()
    {
        // Extract migration name and timestamp from class name
        $className = get_class($this);
        $parts = explode('\\', $className);
        $migrationClass = end($parts);
        
        // Parse migration class name (e.g., "CreateUsersTable")
        $this->timestamp = date('Y_m_d_His');
        $this->name = $migrationClass;
    }

    /**
     * Run the migration
     *
     * @return Sql
     */
    abstract public function up();

    /**
     * Reverse the migration
     *
     * @return Sql
     */
    abstract public function down();

    /**
     * Get migration timestamp
     *
     * @return string
     */
    public function getTimestamp()
    {
        return $this->timestamp;
    }

    /**
     * Get migration name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Create a new table
     *
     * @param string $table
     * @param array $columns
     * @param array $indexes
     * @return Sql
     */
    protected function create($table, array $columns, array $indexes = [])
    {
        return Schema::create($table, $columns, $indexes);
    }

    /**
     * Drop a table if it exists
     *
     * @param string $table
     * @return Sql
     */
    protected function drop($table)
    {
        return Schema::drop($table);
    }

    /**
     * Alter an existing table
     *
     * @param string $table
     * @param array $operations
     * @return Sql
     */
    protected function alter($table, array $operations)
    {
        return Schema::alter($table, $operations);
    }

    /**
     * Rename a table
     *
     * @param string $from
     * @param string $to
     * @return Sql
     */
    protected function rename($from, $to)
    {
        return Schema::rename($from, $to);
    }

    /**
     * Truncate a table
     *
     * @param string $table
     * @return Sql
     */
    protected function truncate($table)
    {
        return Schema::truncate($table);
    }

    /**
     * Check if table exists
     *
     * @param string $table
     * @return bool
     */
    protected function hasTable($table)
    {
        return Schema::hasTable($table);
    }

    /**
     * Check if column exists in table
     *
     * @param string $table
     * @param string $column
     * @return bool
     */
    protected function hasColumn($table, $column)
    {
        return Schema::hasColumn($table, $column);
    }

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

    /**
     * Execute raw SQL and return results
     *
     * @param string $sql
     * @param array $bindings
     * @return Sql
     */
    protected function select($sql, array $bindings = [])
    {
        return Sql::raw($sql, ['bindings' => $bindings, 'type' => 'select']);
    }

    /**
     * Insert data into table
     *
     * @param string $table
     * @param array $data
     * @return Sql
     */
    protected function insert($table, array $data)
    {
        return Schema::insert($table, $data);
    }

    /**
     * Update data in table
     *
     * @param string $table
     * @param array $data
     * @param array $where
     * @return Sql
     */
    protected function update($table, array $data, array $where)
    {
        return Schema::update($table, $data, $where);
    }

    /**
     * Delete data from table
     *
     * @param string $table
     * @param array $where
     * @return Sql
     */
    protected function delete($table, array $where)
    {
        return Schema::delete($table, $where);
    }

    /**
     * Get WordPress table prefix
     *
     * @return string
     */
    protected function getPrefix()
    {
        return Schema::getPrefix();
    }

    /**
     * Get prefixed table name
     *
     * @param string $table
     * @return string
     */
    protected function prefixTable($table)
    {
        return Schema::getTableName($table);
    }
}
