<?php
namespace Core\Database\Operations;

use Core\Database\Schema;
use Core\Database\MigrationOperation;

class AddForeignKey implements MigrationOperation
{
    protected string $table;
    protected string $column;
    protected string $refTable;
    protected string $refColumn;
    protected string $onDelete;

    public function __construct(string $table, string $column, string $refTable, string $refColumn, string $onDelete = 'CASCADE')
    {
        $this->table = $table;
        $this->column = $column;
        $this->refTable = $refTable;
        $this->refColumn = $refColumn;
        $this->onDelete = $onDelete;
    }

    public function up()
    {
        Schema::addForeignKey($this->table, $this->column, $this->refTable, $this->refColumn, $this->onDelete);
    }

    public function down()
    {
        Schema::dropForeignKey($this->table, "fk_{$this->table}_{$this->column}");
    }
}
