<?php

declare(strict_types=1);

namespace App\Framework\Database\StoredProcedure\Exception;

use App\Framework\Database\Exception\DatabaseException;
use App\Framework\Exception\ExceptionContext;

/**
 * Base exception for stored procedure errors
 */
class StoredProcedureException extends DatabaseException
{
    /**
     * Create a new exception for a stored procedure error
     */
    public static function createFromError(
        string $procedureName,
        string $message,
        ?\Throwable $previous = null
    ): self {
        $context = ExceptionContext::forOperation('stored_procedure.error', 'StoredProcedure')
            ->withData(['procedure_name' => $procedureName]);

        return new self(
            "Error in stored procedure '{$procedureName}': {$message}",
            $context,
            500,
            $previous
        );
    }

    /**
     * Create a new exception for a stored procedure execution error
     */
    public static function executionError(
        string $procedureName,
        string $message,
        ?\Throwable $previous = null
    ): self {
        $context = ExceptionContext::forOperation('stored_procedure.execution', 'StoredProcedure')
            ->withData(['procedure_name' => $procedureName]);

        return new self(
            "Error executing stored procedure '{$procedureName}': {$message}",
            $context,
            500,
            $previous
        );
    }

    /**
     * Create a new exception for a stored procedure creation error
     */
    public static function creationError(
        string $procedureName,
        string $message,
        ?\Throwable $previous = null
    ): self {
        $context = ExceptionContext::forOperation('stored_procedure.creation', 'StoredProcedure')
            ->withData(['procedure_name' => $procedureName]);

        return new self(
            "Error creating stored procedure '{$procedureName}': {$message}",
            $context,
            500,
            $previous
        );
    }

    /**
     * Create a new exception for a stored procedure not found error
     */
    public static function notFound(string $procedureName): self
    {
        $context = ExceptionContext::forOperation('stored_procedure.not_found', 'StoredProcedure')
            ->withData(['procedure_name' => $procedureName]);

        return new self(
            "Stored procedure '{$procedureName}' not found",
            $context,
            404
        );
    }
}
