<?php

namespace App\Http\Controllers\Misc;

// Laravel Specifics
use App\Helpers\ClickhouseHandler;
use Illuminate\Http\Request;
use DB;

// Creatia Specifics
use App\Http\Controllers\BaseController;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Throwable;

class GuestController extends BaseController {

    public function get_location_links(Request $_r) {

        try {
            $chh = new ClickhouseHandler(
                config('creatia.clickhouse.database'),
                config('creatia.clickhouse.endpoint'),
                config('creatia.clickhouse.port'),
                config('creatia.clickhouse.user'),
                config('creatia.clickhouse.password')
            );

            $location_links = collect($chh->selectUnpaged("WITH metro_lookup AS (
                    SELECT
                        name as site,
                        metro
                    FROM facilities
                )
                SELECT DISTINCT
                    LEAST(this_side.metro, that_side.metro) AS location_a,
                    GREATEST(this_side.metro, that_side.metro) AS location_b
                FROM
                    interface_mapping_backhaul FINAL
                JOIN
                    metro_lookup AS this_side ON this_side.site = facility
                JOIN
                    metro_lookup AS that_side ON that_side.site = far_side_facility
                WHERE
                    this_side.metro != 'mus' AND that_side.metro != 'mus' AND location_a != location_b
            ")->get());

            return response()->json($location_links, 200);            
        }
        catch(Throwable $t) {
            return response()->json([ 'NO LINKS FOUND' ], 500);
        }

    }

    public function get_tilemap(Request $_r, int $_a, int $_b, int $_c, string $_theme) {

        $validity = Validator::make([
            'a' => $_a,
            'b' => $_b,
            'c' => $_c,
            'theme' => $_theme
        ], [
            'a' => [ 'required', 'integer' ],
            'b' => [ 'required', 'integer' ],
            'c' => [ 'required', 'integer' ],
            'theme' => [ 'required', Rule::in('dark', 'light') ]
        ]);

        if($validity->fails()) {
            return response()->json([], 400);
        }

        $cached = ($_a >= 3 && $_a <= 5) ? Redis::get("tile:{$_theme}:{$_a}:{$_b}:{$_c}") : null;

        if ($cached) {
            return response(base64_decode($cached), 200)
                ->header('Content-Type', 'image/png')
                ->header('Cache-Control', 'public, max-age=86400');
        }

        \Log::debug("Tile that wasn't cached is being retrieved");

        $upstream = Http::timeout(10)->get($_theme === 'dark' ? "https://basemaps.cartocdn.com/rastertiles/dark_all/$_a/$_b/$_c.png" : "https://basemaps.cartocdn.com/rastertiles/voyager/$_a/$_b/$_c.png", [
            'key' => env('MAP_KEY')
        ]);

        if (!$upstream->successful()) {
            return response()->json([], 502);
        }

        return response($upstream->body(), 200)
            ->header('Content-Type', $upstream->header('Content-Type') ?: 'image/png')
            ->header('Cache-Control', 'public, max-age=86400');

    }

}
