<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use Hypervel\Http\Request;
use Hypervel\Support\Facades\Response;
use Hypervel\Support\Facades\File;
use Hypervel\Support\Facades\Cache;

class PageMemoryController
{
    public static function getPageInMemory(string $viewName, array $data = [], int $ttlSeconds = 100)
    {
        $dataHash = sha1(json_encode($data));
        $cacheKey = 'Cache:Pages:Views:' . $viewName . '-------' . $dataHash;

        return Response::html(
            cache()->remember($cacheKey, $ttlSeconds, function () use ($viewName, $data) {
                return view($viewName, $data)->render();
            })
        );

    }

    public static function readAssetInMemory(string $assetFilename, int $ttlSeconds = 10000, $asset_folder = null): ?string
    {
        $assetFilename = ltrim($assetFilename, '/\\');
        $cacheKey = 'Cache:Assets:Static:' . $assetFilename;
        if (!$asset_folder) {
            $filePath = storage_path('app/cache/static/' . $assetFilename);
        } else {
            $filePath = $asset_folder . $assetFilename;
        }

        $data = Cache::get($cacheKey);

        if ($data) {
            return $data['content'];
        }

        if (!File::exists($filePath)) {
            return null;
        }

        $fileContent = file_get_contents($filePath);
        $mimeType = File::mimeType($filePath) ?? 'application/octet-stream';

        $data = [
            'content' => $fileContent,
            'mime' => $mimeType,
        ];

        Cache::put($cacheKey, $data, $ttlSeconds);

        return $fileContent;
    }

    public static function readPublicAssetInMemory($assetFilename, $ttlSeconds = 10000)
    {
        $asset_folder = public_path('static/');
        return self::readAssetInMemory($assetFilename, $ttlSeconds, $asset_folder);
    }

    public static function getAssetInMemory(string $assetFilename, int $ttlSeconds = 10000)
    {
        $assetFilename = ltrim($assetFilename, '/\\');
        $cacheKey = 'Cache:Assets:Static:' . $assetFilename;
        $filePath = storage_path('app/cache/static/' . $assetFilename);

        $data = Cache::get($cacheKey);
        if ($data) {
            return Response::raw($data['content'])
                ->withHeader('Content-Type', $data['mime'] ?? 'text/css')
                ->withHeader('Content-Length', (string) strlen($data['content']))
                ->withStatus(200);
        }


        try {
            $fileContent = file_get_contents($filePath);
            $mimeType = File::mimeType($filePath) ?? 'application/octet-stream';

            // Cache content + mime type
            $data = [
                'content' => $fileContent,
                'mime' => $mimeType,
            ];
            Cache::put($cacheKey, $data, $ttlSeconds);

            return Response::raw($data['content'])
                ->withHeader('Content-Type', $data['mime'] ?? 'text/css')
                ->withHeader('Content-Length', (string) strlen($data['content']))
                ->withStatus(200);

        } catch (\Throwable $th) {
            return Response::withStatus(404, 'Not Found ' . $th->getMessage());
        }
    }
}
