<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\SettingsService;
use App\Services\RenderService;
use App\Services\MaintenanceService;
use App\Services\AuthService;

class SettingsAPIController extends Controller
{
    protected $settings;
    protected $render;
    protected $mainteance;
    protected $auth;

    public function __construct(
        SettingsService $settingsService,
        RenderService $renderService,
        MaintenanceService $maintenanceService,
        AuthService $authService
    ) {
        $this->settings    = $settingsService;
        $this->render      = $renderService;
        $this->maintenance = $maintenanceService;
        $this->auth        = $authService;
    }

    public function publish(Request $request)
    {
        $result = $this->render->publishAll();
        return response()->json($result)->header('Content-Type', 'application/json');
    }

    public function sync(Request $request)
    {
        $body   = json_decode($request->getContent());
        $result = $this->settings->sync($body);
        return response()->json($result)->header('Content-Type', 'application/json');
    }

    public function navSync(Request $request)
    {
        $body   = json_decode($request->getContent());
        $result = $this->settings->navSync($body);
        return response()->json($result)->header('Content-Type', 'application/json');
    }

    public function createBackup(Request $request)
    {
        return response()->json($this->maintenance->createBackup())->header('Content-Type', 'application/json');
    }

    public function downloadBackup(Request $request)
    {
        if ($this->auth::status()) {
            $latest = $this->settings->getGlobal()['last_backup'];
            $file   = 'backup-' . $latest . '.zip';
            return response()->download('../content/backups/' . $file, $file, ['Content-Type: application/zip']);
        }
    }

    //init stuff
    public function setupFresh(Request $request)
    {
        $body = json_decode($request->getContent());
        dd($body);
    }
}