<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class ApiSettingController extends Controller
{

    public function index(): View
    {
        return view('pages.user.api.credentials');
    }

    public function renew(): RedirectResponse
    {
        $user = Auth::user();
        
        $user->update([
            'api_id' => 'API-' . Str::upper(Str::random(10)),
            'api_key' => Str::random(32),
        ]);

        $message = $user->wasChanged('api_id') 
            ? 'API credentials generated successfully!' 
            : 'API credentials renewed successfully!';

        return redirect()
            ->route('user.api.credentials')
            ->with('success', $message);
    }
}
