<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Symfony\Component\HttpFoundation\Response;

class StaticController extends Controller
{
    /**
     * Download the full privacy policy for the current locale
     */
    public function downloadPrivacyPolicy(Request $request)
    {
        $locale = app()->getLocale();
        $platform = config('timebank-cc.platform_name', env('TIMEBANK_CONFIG', 'timebank_cc'));

        // Get the base path for GDPR references
        $basePath = base_path("references/gdpr/{$platform}");

        // Find the most recent date folder
        $dateFolders = File::directories($basePath);

        if (empty($dateFolders)) {
            abort(404, 'Privacy policy not found');
        }

        // Sort folders by name (dates in YYYY-MM-DD format) descending
        rsort($dateFolders);
        $latestFolder = $dateFolders[0];

        // Construct the file path
        $fileName = "privacy-policy-FULL-{$locale}.md";
        $filePath = "{$latestFolder}/{$fileName}";

        if (!File::exists($filePath)) {
            abort(404, 'Privacy policy not found for this language');
        }

        // Return the file as a download
        return response()->download($filePath, $fileName, [
            'Content-Type' => 'text/markdown',
        ]);
    }
}
