<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\License;
use Illuminate\Http\Request;
use App\Models\Application;
use App\Models\TransactionLigdicash;

class TransactionLigdicashController extends Controller
{
    public function store(Request $request)
    {
        // Valider les données de la requête
        $validatedData = $request->validate([
            'response_code' => 'required|string',
            'token' => 'required|string',
            'response_text' => 'nullable|string',
            'description' => 'nullable|string',
            'custom_data.transaction_id' => 'nullable|string',
            'custom_data.Plateforme' => 'nullable|string',
            'custom_data.logfile' => 'nullable|string',
            'wiki' => 'nullable|string',
        ]);

        // Créer une nouvelle instance de TransactionLigdicash
        $transaction = new TransactionLigdicash();
        $transaction->response_code = $validatedData['response_code'];
        $transaction->token = $validatedData['token'];
        $transaction->response_text = isset($validatedData['response_text']) ? $validatedData['response_text'] : null;
        $transaction->description = isset($validatedData['description']) ? $validatedData['description'] : '';
        $transaction->transaction_id = isset($validatedData['custom_data']['transaction_id']) ? $validatedData['custom_data']['transaction_id'] : null;
        $transaction->plateforme = isset($validatedData['custom_data']['Plateforme']) ? $validatedData['custom_data']['Plateforme'] : null;
        $transaction->logfile = isset($validatedData['custom_data']['logfile']) ? $validatedData['custom_data']['logfile'] : null;
        $transaction->wiki = isset($validatedData['wiki']) ? $validatedData['wiki'] : null;

        // Persister l'instance dans la base de données
        $transaction->save();

        // Retourner une réponse JSON
        return response()->json(['message' => 'Transaction saved successfully', 'data' => $transaction], 201);
    }
}
