<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Controllers\Controller;
use App\Models\OperationsAlert;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
final class OperationsAlertController extends Controller {
 public function index(Request $r){$q=OperationsAlert::query()->latest('id');if($r->boolean('open',true))$q->whereNull('acknowledged_at');if($r->filled('severity'))$q->where('severity',$r->string('severity'));if($r->filled('code'))$q->where('code',$r->string('code'));$page=$q->paginate(min(max($r->integer('per_page',25),1),100));return ApiResponse::success(collect($page->items())->map(fn($a)=>$this->resource($a))->values(),200,['pagination'=>['page'=>$page->currentPage(),'per_page'=>$page->perPage(),'total'=>$page->total(),'last_page'=>$page->lastPage()]]);} 
 public function acknowledge(Request $r,string $id){$a=OperationsAlert::find($id);if(!$a)return ApiResponse::error('ALERT_NOT_FOUND','The requested operations alert does not exist.',404);$a->update(['acknowledged_at'=>now()]);return ApiResponse::success($this->resource($a->fresh()));}
 private function resource(OperationsAlert $a):array{return ['id'=>$a->id,'severity'=>$a->severity,'code'=>$a->code,'subject_type'=>$a->subject_type,'subject_id'=>$a->subject_id,'message'=>$a->message,'context'=>$a->context??new \stdClass(),'created_at'=>$a->created_at?->utc()->toIso8601String(),'acknowledged_at'=>$a->acknowledged_at?->utc()->toIso8601String()];}
}