<?php

namespace App\Controllers;

use JetBrains\PhpStorm\NoReturn;

class Media extends BaseController
{
    public function index(): void
    {

        $target_file = "/var/www/html/SITEFILES/TEST/test.jpg";
        $filename = $target_file;
        $file_extension = strtolower(substr(strrchr($filename, "."), 1));
        $ctype = "image/jpeg";
        switch ($file_extension) {
            case "gif":
                $ctype = "image/gif";
                break;
            case "png":
                $ctype = "image/png";
                break;
            case "jpeg":
            case "jpg":
                $ctype = "image/jpeg";
                break;
            case "svg":
                $ctype = "image/svg+xml";
                break;
            case "mp4":
                $ctype = "application/octet-stream";
                break;
            // case "mp4": $ctype="video/mp4"; break;
            default:
        }

        $selectedFile = $target_file;
        header('Content-type: ' . $ctype);
        readfile($selectedFile);
        exit();


    }

    #[NoReturn] public function imageGate(): void
    {
        $uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
        $segLen = count($uriSegments);
        //var_dump($uriSegments);

        $filename = $this->imageSets($uriSegments[$segLen - 1]);
        $target_file = "/var/www/html/SITEFILES/TEST/" . $filename;
        $file_extension = strtolower(substr(strrchr($filename, "."), 1));
        $ctype = "image/jpeg";
        switch ($file_extension) {
            case "gif":
                $ctype = "image/gif";
                break;
            case "png":
                $ctype = "image/png";
                break;
            case "jpeg":
            case "jpg":
                $ctype = "image/jpeg";
                break;
            case "svg":
                $ctype = "image/svg+xml";
                break;
            case "mp4":
                $ctype = "application/octet-stream";
                break;
            // case "mp4": $ctype="video/mp4"; break;
            default:
        }

        $selectedFile = $target_file;
        header('Content-type: ' . $ctype);
        readfile($selectedFile);
        exit();
    }

    //
    #[NoReturn] public function WebsiteGate(): void
    {

        $uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
        $segLen = count($uriSegments);
//        var_dump($uriSegments);
//        0 => string '' (length=0)
//        1 => string 'website' (length=7)
//        2 => string '4bdc1594-a0d4-4eba-9dff-9e62f5c28d69' (length=36)
//        3 => string 'coronary-artery-disease.png' (length=27)

        $filename = $uriSegments[$segLen - 1];
        $webFileUid = $uriSegments[$segLen - 2];
        $fileDetailResult = $this->getWebFileDetails($webFileUid);

        $fileDetails = $fileDetailResult[0];
        //var_dump($fileDetails);
//'id' => string '12' (length=2)
//'uid' => string '4bdc1594-a0d4-4eba-9dff-9e62f5c28d69' (length=36)
//'member_id' => string '20' (length=2)
//'member_uid' => string '2f1f9c8a-d50b-4fac-9de3-081bbc07cc64' (length=36)
//'file_group' => string 'WEBSITE' (length=7)
//'filename' => string 'coronary-artery-disease.png' (length=27)
//'save_filename' => string '1767224858-coronary-artery-disease.png' (length=38)
//'file_type' => string 'png' (length=3)
//'file_size' => string '0' (length=1)
//'status' => string '1' (length=1)
//'added' => string '2025-12-31 18:47:38.905934' (length=26)
//'updated' => string '2025-12-31 18:47:38.905943' (length=26)

        //     exit;
///var/www/html/uploads/uploads/DEV/WEBSITE/W0000000020/1767055095-practice-in-ai_1.jpg
        // $target_file = "/var/www/html/uploads/uploads/DEV/WEBSITE/W0000000020/1767055095-practice-in-ai_1.jpg";
        $target_file = $_ENV['UPLOAD_FOLDER'] . "/" . $fileDetails['file_group'] . "/W" . str_pad($fileDetails['member_id'], 10, '0', STR_PAD_LEFT) . "/" . $fileDetails['save_filename'];
        //$target_file = "/var/www/html/SITEFILES/TEST/" . $filename;
        log_message('info', "TARGET:::FILE-> $target_file");

        $file_extension = $fileDetails['file_type']; // strtolower(substr(strrchr($filename, "."), 1));
        $ctype = "image/jpeg";
        switch ($file_extension) {
            case "gif":
                $ctype = "image/gif";
                break;
            case "png":
                $ctype = "image/png";
                break;
            case "jpeg":
            case "jpg":
                $ctype = "image/jpeg";
                break;
            case "svg":
                $ctype = "image/svg+xml";
                break;
            case "mp4":
                $ctype = "application/octet-stream";
                break;
            default:
        }

        $selectedFile = $target_file;
        header('Content-type: ' . $ctype);
        readfile($selectedFile);
        exit();
    }

    private function getWebFileDetails($webFileUid): array
    {
        try {
            $sqlP = "SELECT * FROM members_webfiles WHERE uid::text = '$webFileUid' ";
            $query = $this->db->query($sqlP);
            $row = $query->getResultArray();
            if (isset($row)) {
                return $row;
            }
        } catch (Exception $e) {
            // Handle the specific exception
            //echo "Caught exception: " . $e->getMessage() . "\n";
            log_message('error', 'Error in getWebFileDetails :: :: ' . $e->getMessage());
        } finally {
            // Cleanup code, always runs
            log_message('info', "Consider CACHE IN getWebFileDetails - $webFileUid");
        }
        return [];
    }

    private function imageSets($selImg): string
    {
        $image_set = [
            "merms-profile" => "merms-profile.png",
            "test" => "test.jpg",
            "about-kintcare" => "about-kintcare.png",
            "about8-image" => "about8-image.png",
            "about7-image" => "about7-image.png",
            "about6-image1" => "about6-image1.png",
            "about6-image2" => "about6-image2.png",
            "about5-image3" => "about5-image3.png",
            "about5-image2" => "about5-image2.png",
            "about4-image3" => "about4-image3.png",
            "about5-image1" => "about5-image1.png",
            "about3-image1" => "about3-image1.png",
            "about3-image2" => "about3-image2.png",
            "about4-image1" => "about4-image1.png",
            "about10-image" => "about10-image.png",
            "about2-image1" => "about2-image1.png",
            "about2-image2" => "about2-image2.png",
            "about-page-sec1-image1" => "about-page-sec1-image1.png",
            "about-page-sec1-image2" => "about-page-sec1-image2.png",
            "about-choose-sec-image2" => "about-choose-sec-image2.png",
            "about-choose-sec-image3" => "about-choose-sec-image3.png",
            "about-choose-sec-image1" => "about-choose-sec-image1.png",
        ];
        return $image_set[$selImg];
    }

}
