<?php

namespace App\Http\Controllers\Admin\TopUp;

use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Model;

abstract class BasePaymentController extends Controller
{
    /**
     * Get callback URLs for payment gateway.
     */
    abstract protected function getCallbackUrls(): array;

    /**
     * Get payment gateway name.
     */
    abstract protected function getGatewayName(): string;

    /**
     * Get payment methods available for this gateway.
     */
    abstract protected function getPaymentMethods(): array;

    /**
     * Get settings model class.
     */
    abstract protected function getSettingsModel(): string;

    /**
     * Get transaction model class.
     */
    abstract protected function getTransactionModel(): string;

    /**
     * Get settings instance.
     */
    protected function getSettings(): ?Model
    {
        $modelClass = $this->getSettingsModel();
        return $modelClass::first();
    }

    /**
     * Check if settings are configured.
     */
    protected function isConfigured(): bool
    {
        $settings = $this->getSettings();
        return $settings && $settings->isConfigured();
    }

    /**
     * Merge validated data with callback URLs.
     */
    protected function mergeWithCallbackUrls(array $validated): array
    {
        return array_merge($validated, $this->getCallbackUrls());
    }

    /**
     * Update or create settings.
     */
    protected function saveSettings(array $data): void
    {
        $modelClass = $this->getSettingsModel();
        $modelClass::updateOrCreate(
            ['id' => $modelClass::first()?->id ?? 1],
            $data
        );
    }

    /**
     * Find transaction by ID.
     */
    protected function findTransaction(int $id): ?Model
    {
        $modelClass = $this->getTransactionModel();
        return $modelClass::find($id);
    }

    /**
     * Format success message for transaction sync.
     */
    protected function getSyncSuccessMessage(bool $goldAdded = false): string
    {
        $gateway = $this->getGatewayName();
        return $goldAdded 
            ? "Transaction synced and gold added to user."
            : "Transaction synced successfully.";
    }

    /**
     * Format error message for transaction sync.
     */
    protected function getSyncErrorMessage(string $error): string
    {
        return "Error syncing transaction: {$error}";
    }

    /**
     * Get common validation rules for settings update.
     */
    protected function getSettingsValidationRules(): array
    {
        return [
            'production_mode' => 'required|boolean',
            'gold_rate' => 'required|integer|min:1',
            'min_amount' => 'required|integer|min:1000',
            'max_amount' => 'required|integer|min:10000',
            'is_active' => 'required|boolean',
        ];
    }
}
