<?php

namespace App\Controllers;

use App\Services\ProfileService;

class SettingsController extends BaseController
{
    public function index()
    {
        $profiles = new ProfileService();
        $user     = $profiles->userFromSession();
        if ($user === null) {
            return redirect()->to('/sign-in');
        }

        return view('settings/index', [
            'title'     => 'Settings',
            'activeNav' => 'menu',
            'email'     => (string) $user['email'],
            'values'    => $profiles->formValues($user),
            'errors'    => [],
        ]);
    }

    public function updateProfile()
    {
        $profiles = new ProfileService();
        $user     = $profiles->userFromSession();
        if ($user === null) {
            return redirect()->to('/sign-in');
        }

        $raw    = $profiles->collect((array) $this->request->getPost());
        $errors = $profiles->validate($raw);
        if ($errors !== []) {
            return view('settings/index', [
                'title'     => 'Settings',
                'activeNav' => 'menu',
                'email'     => (string) $user['email'],
                'values'    => $profiles->formValues($raw),
                'errors'    => $errors,
            ]);
        }

        if (! $profiles->save((int) $user['id'], $profiles->normalized($raw), completeOnboarding: false)) {
            return redirect()->back()->with('error', ProfileService::MSG_SAVE_FAIL);
        }

        return redirect()->to('/settings')->with('success', 'Profile saved.');
    }

    public function onboarding()
    {
        $profiles = new ProfileService();
        $user     = $profiles->userFromSession();
        if ($user === null) {
            return redirect()->to('/sign-in');
        }

        if ((int) $user['profile_onboarding_completed'] === 1) {
            return redirect()->to('/dashboard');
        }

        return view('profile/onboarding', [
            'title'  => 'Let us serve you better!',
            'email'  => (string) $user['email'],
            'values' => $profiles->formValues($user),
            'errors' => [],
        ]);
    }

    public function completeOnboarding()
    {
        $profiles = new ProfileService();
        $user     = $profiles->userFromSession();
        if ($user === null) {
            return redirect()->to('/sign-in');
        }

        if ((int) $user['profile_onboarding_completed'] === 1) {
            return redirect()->to('/dashboard');
        }

        if ((string) $this->request->getPost('skip') === '1') {
            if (! $profiles->markOnboardingComplete((int) $user['id'])) {
                return redirect()->back()->with('error', ProfileService::MSG_SAVE_FAIL);
            }

            return redirect()->to('/dashboard');
        }

        $raw    = $profiles->collect((array) $this->request->getPost());
        $errors = $profiles->validate($raw);
        if ($errors !== []) {
            return view('profile/onboarding', [
                'title'  => 'Let us serve you better!',
                'email'  => (string) $user['email'],
                'values' => $profiles->formValues($raw),
                'errors' => $errors,
            ]);
        }

        if (! $profiles->save((int) $user['id'], $profiles->normalized($raw), completeOnboarding: true)) {
            return redirect()->back()->with('error', ProfileService::MSG_SAVE_FAIL);
        }

        return redirect()->to('/dashboard');
    }
}
