<?php
namespace app\controllers;

use app\app;
use app\models\magic_links;
use app\models\users;

class magic_link
{
    public static function index()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') { // using a 6-digit code
            $code = $_POST['code'] ?? null;
            if (! $code) {
                $_SESSION['error'] = "Invalid or expired code.";
                header('Location: /account/verify');
                exit;
            } else {
                $link = magic_links::validateCode($code);
                if ($link) { // the code is valid
                    $user = $link['user_id'] ? users::getById($link['user_id']) : users::getByEmail($link['email']);
                    if ($user) { // this email is registered or associated with a user
                        $user_replacing = users::getByReplaceEmailToken($link['token']);
                        if ($user_replacing && $user_replacing['id'] == $link['user_id']) { // user is replacing their email
                            users::updateEmailById($user_replacing['id'], $link['email']);
                            $_SESSION['user_email'] = $link['email'];
                            $_SESSION['user_id']    = $user_replacing['id'];
                            if (! $user_replacing['verified']) {
                                users::verify($link['email']);
                            }
                            header('Location: /account');
                            exit;
                        } else { // user is logging in
                            $_SESSION['user_email'] = $link['email'];
                            $_SESSION['user_id']    = $user['id'];
                            if (! $user['verified']) {
                                users::verify($link['email']);
                            }
                            header('Location: /account');
                            exit;
                        }
                    } else { // new user signup
                        $_SESSION['user_email'] = $link['email'];
                        header('Location: /account/signup');
                        exit;
                    }
                } else { // the code is not valid
                    $_SESSION['error'] = "Invalid or expired verification code.";
                    header('Location: /account/verify');
                    exit;
                }
            }
        } else { // using a link with a token
            $token = $_GET['token'] ?? null;
            if (! $token) {
                $_SESSION['error'] = "Invalid or expired link.";
                header('Location: /account/login');
                exit;
            } else {
                $link = magic_links::validateToken(token: $token);
                if (! $link) {
                    $_SESSION['error'] = "Invalid or expired link.";
                    header('Location: /account/login');
                    exit;
                }
                $user = $link['user_id'] ? users::getById($link['user_id']) : users::getByEmail($link['email']);
                if ($user) { // this email is registered or is associated with a user
                    $user_replacing = users::getByReplaceEmailToken($token);
                    if ($user_replacing && $user_replacing['id'] == $link['user_id']) { // user is replacing their email
                        users::updateEmailById($user_replacing['id'], $link['email']);
                        $_SESSION['user_email'] = $link['email'];
                        $_SESSION['user_id']    = $user_replacing['id'];
                        if (! $user_replacing['verified']) {
                            users::verify($link['email']);
                        }
                        header('Location: /account');
                        exit;
                    } else { // user is logging in
                        $_SESSION['user_email'] = $link['email'];
                        $_SESSION['user_id']    = $user['id'];
                        if (! $user['verified']) {
                            users::verify($link['email']);
                        }
                        header('Location: /account');
                        exit;
                    }
                } else { // new user signup
                    $_SESSION['user_email'] = $link['email'];
                    header('Location: /account/signup');
                    exit;
                }
            }
        }
    }
}
