<?php namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\GiteaWebhookRequest;
use App\Jobs\ProcessDeployment;
use App\Models\GiteaActionLog;
use Illuminate\Http\JsonResponse;

class GiteaWebhookController extends Controller
{
    public function store(GiteaWebhookRequest $request): JsonResponse
    {
        $data = $request->validated();

        $log = GiteaActionLog::query()->create($data);

        ProcessDeployment::dispatch(
            $data['project_name'],
            $data['branch_name'],
            $data['commit_hash'],
            $log->id
        );

        return response()->json(['message' => 'Success'], 201);
    }
}
