<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Models\ImmigoProLegalAgreement;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class ImmigoProLegalAgreementController extends Controller
{
    /**
     * Get the legal agreement (only one exists)
     */
    public function getAgreement()
    {
        try {
            $agreement = ImmigoProLegalAgreement::first();
            
            if (!$agreement) {
                // Return empty content if no agreement exists yet
                return response()->json([
                    'data' => [
                        'id' => null,
                        'content_en' => '',
                        'content_fr' => ''
                    ]
                ]);
            }

            return response()->json([
                'data' => $agreement
            ]);
        } catch (\Exception $e) {
            Log::error('Get legal agreement error: ' . $e->getMessage());
            return response()->json([
                'message' => 'Failed to retrieve legal agreement'
            ], 500);
        }
    }

    /**
     * Update or create the legal agreement (English)
     */
    public function updateEnglish(Request $request)
    {
        try {
            $validated = $request->validate([
                'content' => 'required|string'
            ]);

            // Get or create the single agreement record
            $agreement = ImmigoProLegalAgreement::first();
            
            if (!$agreement) {
                $agreement = new ImmigoProLegalAgreement();
            }

            $agreement->content_en = $validated['content'];
            $agreement->save();

            return response()->json([
                'message' => 'English agreement updated successfully',
                'data' => $agreement
            ]);
        } catch (\Exception $e) {
            Log::error('Update English agreement error: ' . $e->getMessage());
            return response()->json([
                'message' => 'Failed to update English agreement'
            ], 500);
        }
    }

    /**
     * Update or create the legal agreement (French)
     */
    public function updateFrench(Request $request)
    {
        try {
            $validated = $request->validate([
                'content' => 'required|string'
            ]);

            // Get or create the single agreement record
            $agreement = ImmigoProLegalAgreement::first();
            
            if (!$agreement) {
                $agreement = new ImmigoProLegalAgreement();
            }

            $agreement->content_fr = $validated['content'];
            $agreement->save();

            return response()->json([
                'message' => 'French agreement updated successfully',
                'data' => $agreement
            ]);
        } catch (\Exception $e) {
            Log::error('Update French agreement error: ' . $e->getMessage());
            return response()->json([
                'message' => 'Failed to update French agreement'
            ], 500);
        }
    }
}
