<?php
namespace App\Http\Controllers\Api\V1;
use App\Application\Services\ApiCredentialService;
use App\Http\Controllers\Controller;
use App\Models\ApiKey;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
final class ApiKeyController extends Controller { public function index(Request $r){return ApiResponse::success($r->user()->apiKeys()->latest()->get());} public function store(Request $r,ApiCredentialService $s){$d=$r->validate(['environment'=>'required|in:sandbox,live','scopes'=>'required|array|min:1','scopes.*'=>'in:payments:read,payments:write,balances:read,payouts:read,payouts:write,webhooks:write,merchants:read,merchants:write,wallets:read,wallets:write']);$issued=$s->issue($r->user(),$d['environment'],$d['scopes']);return ApiResponse::success(['id'=>$issued['api_key']->id,'public_key'=>$issued['public_key'],'secret'=>$issued['secret'],'warning'=>'Store this secret now. It will not be shown again.'],201);} public function rotate(Request $r,ApiKey $apiKey,ApiCredentialService $s){abort_unless($apiKey->merchant_id===$r->user()->id,404);$apiKey->update(['status'=>'revoked']);$issued=$s->issue($r->user(),$apiKey->environment,$apiKey->scopes);return ApiResponse::success(['public_key'=>$issued['public_key'],'secret'=>$issued['secret'],'warning'=>'Store this secret now.']);} public function destroy(Request $r,ApiKey $apiKey){abort_unless($apiKey->merchant_id===$r->user()->id,404);$apiKey->update(['status'=>'revoked']);return response()->noContent();} }

