<?php

namespace brain\utility;

use brain\data\Settings;

class Maintenance
{
    public function __construct()
    {
    }

    public static function makeBackup()
    {
        //make sure back directory is there
        if (!is_dir("../config/backups")) {
            mkdir("../config/backups", 0755, true);
        }
        //creat backup zip
        $zip = new ZipArchive();
        $zip->open(
            "../config/backups/latest_back.zip",
            ZipArchive::CREATE | ZipArchive::OVERWRITE
        );
        //gather data and path info for md pages
        $pagePath = "../content/pages";
        $yearPaths = glob($pagePath . "/*", GLOB_ONLYDIR);
        foreach ($yearPaths as $years) {
            $year = explode("/", $years);
            //grap the index and save it
            if (trim($year[3]) == "start") {
                $options = [
                "add_path" => "content/pages/" . $year[3] . "/",
                "remove_all_path" => true,
                ];
                $zip->addGlob($years . "/*.md", GLOB_BRACE, $options);
            }
            $monthsPath = glob($pagePath . "/" . $year[3] . "/*", GLOB_ONLYDIR);
            foreach ($monthsPath as $months) {
                $month = explode("/", $months);
                //once info is collected, add md pages to zip
                $options = [
                "add_path" => "content/pages/" . $year[3] . "/" . $month[4] . "/",
                "remove_all_path" => true,
                ];
                $zip->addGlob($months . "/*.md", GLOB_BRACE, $options);
            }
        }

        //gather data and path info for blog images
        $blogImagesPath = "../public/assets/images/blog";
        $yearPaths = glob($blogImagesPath . "/*", GLOB_ONLYDIR);
        foreach ($yearPaths as $years) {
            $year = explode("/", $years);
            $monthsPath = glob($blogImagesPath . "/" . $year[5] . "/*", GLOB_ONLYDIR);
            foreach ($monthsPath as $months) {
                $month = explode("/", $months);
                //once info is collected, add images pages to zip
                $options = [
                "add_path" =>
                "public/assets/images/blog/" . $year[5] . "/" . $month[6] . "/",
                "remove_all_path" => true,
                ];
                $zip->addGlob($months . "/*.*", GLOB_BRACE, $options);
            }
        }

        //gather data and path info for user images
        $userImagesPath = "../public/assets/images/user";
        $yearPaths = glob($userImagesPath . "/*", GLOB_ONLYDIR);
        foreach ($yearPaths as $years) {
            $year = explode("/", $years);
            $monthsPath = glob($userImagesPath . "/" . $year[5] . "/*", GLOB_ONLYDIR);
            foreach ($monthsPath as $months) {
                $month = explode("/", $months);
                //once info is collected, add images pages to zip
                $options = [
                "add_path" =>
                "public/assets/images/user/" . $year[5] . "/" . $month[6] . "/",
                "remove_all_path" => true,
                ];
                $zip->addGlob($months . "/*.*", GLOB_BRACE, $options);
            }
        }

        //add directory for settings and save them
        $zip->addEmptyDir("settings");
        $zip->addFile("../config/settings.json", "settings/settings.json");
        $zip->addFile("../config/folks.json", "settings/folks.json");
        $zip->addFile("../config/tags.json", "settings/tags.json");
        //save zip file
        $zip->close();

        //update settings file with latest back up date
        $updated = new \Moment\Moment();
        Settings::updateGlobalData(
            "last_backup",
            $updated->format("Y-m-d\TH:i:sP")
        );

        $result = ["message" => "Backup created. THIS IS A SAFE SPACE!"];
        return $result;
    }
}