<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller as BaseController;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;






class Controller extends BaseController
{

  
    public $user;

    
    public function handleError(\Exception $e, string $page): JsonResponse
    {
        $message = "Error occured on {$page}: " . $e->getMessage();
        \Log::error($message);
        return response()->json([
            'status' => false,
            'error' => $e->getMessage()
        ], 500);
    }

    // Centralized response structure
    public function sendSuccess(array $message): JsonResponse
    {
        return response()->json(['status' => false, 'message' => $message]);
    }

    public function sendError(array $message): JsonResponse
    {
        return response()->json(['status' => false, 'error' => $message]);
    }

    public function manageSession(array $data, $forget = false): void
    {
        if ($forget) {
            session()->forget($data);
        } else {
            foreach ($data as $key => $value) {
                session()->put($key, $value);
            }
        }
    }

    protected function formatUserResponse(User $user): array
    {
        $account = $user->account ?? $user->account()->first();
        return [
            'id' => hash_of($user->id),
            'username' => $user->username,
            'full_name' => ucfirst($user->full_name),
            'display_name' => $user->display_name,
            'profile_photo' => $user->photo ? asset('uploads/profile/' . $user->photo) : '/uploads/images/human.png',
            'email' => $user->email,
            'role' => $user->role->value,
            'role_label' => $user->role_label,
            'currency' => $user->currency,
            'currency_symbol' => currency_sign($user->currency),
            'balance' => optional($account)->balance ?? 0,
            'mining' => optional($account)->mining ?? 0,
            'profit' => optional($account)->profit ?? 0,
            'referral' => optional($account)->referral ?? 0,
            'country' => $user->country,
            'email_status' => (bool) $user->email_verify,
            'kyc_status' => $user->kyc_verify,
            'status' => $user->status->value,
            'status_label' => $user->status_label,
            'notifications_count' => $user->unreadNotifications()->count(),
        ];
    }

    protected function safeResponse(callable $callback): JsonResponse
    {
        try {
            $settings = ['settings' => 
                            [
                                'siteName' => system_settings('site_name'),
                                'siteLogo' => system_settings('site_logo'),
                                'siteFavicon' => system_settings('site_favicon'),
                            ]
                        ];
            return response()->json([
                'status' => true,
                'data' => array_merge($callback() , $settings)
            ]);
        } catch (\Exception $e) {
            \Log::error("API Error: " . $e->getMessage(), [
                'trace' => $e->getTraceAsString()
            ]);

            return response()->json([
                'status' => false,
                'error' => $e->getMessage()
            ], 422);
        }
    }


}