<?php
namespace app\controllers;

use app\controllers\lost;
use app\models\transactions;
use app\models\users;

class transaction
{
    public static function view($defaults, $txid)
    {
        $tx = transactions::getById($txid);
        if (! $tx) {
            lost::index($defaults);
        }
        echo $GLOBALS['twig']->render('lib/pages/index.twig', array_merge($defaults, [
            'child_template' => 'transaction.twig',
            'page_title'     => 'Transaction Reciept #' . $txid,
            'tx'             => $tx,
            'user'           => users::getById($tx['user_id']),
            'breadcrumbs'    => [
                [
                    'url'   => "/transaction/" . $txid,
                    'title' => 'Transaction Reciept',
                ],
            ],
        ]));
    }

    public static function users($defaults)
    {
        echo $GLOBALS['twig']->render('lib/pages/index.twig', array_merge($defaults, [
            'child_template' => 'admin/users.twig',
            'page_title'     => $_ENV['APP_NAME'] . ' Users',
            'breadcrumbs'    => [
                [
                    'url'   => '/admin',
                    'title' => 'Admin',
                ],
                [
                    'url'   => '/admin/users',
                    'title' => 'Users',
                ],
            ],
        ]));
    }

    public static function orders($defaults)
    {
        echo $GLOBALS['twig']->render('lib/pages/index.twig', array_merge($defaults, [
            'child_template' => 'admin/orders.twig',
            'page_title'     => $_ENV['APP_NAME'] . ' Orders',
            'breadcrumbs'    => [
                [
                    'url'   => '/admin',
                    'title' => 'Admin',
                ],
                [
                    'url'   => '/admin/orders',
                    'title' => 'Orders',
                ],
            ],
        ]));
    }

    public static function returns($defaults)
    {
        echo $GLOBALS['twig']->render('lib/pages/index.twig', array_merge($defaults, [
            'child_template' => 'admin/returns.twig',
            'page_title'     => $_ENV['APP_NAME'] . ' Returns',
            'breadcrumbs'    => [
                [
                    'url'   => '/admin',
                    'title' => 'Admin',
                ],
                [
                    'url'   => '/admin/returns',
                    'title' => 'Returns',
                ],
            ],
        ]));
    }

    public static function transactions($defaults)
    {
        echo $GLOBALS['twig']->render('lib/pages/index.twig', array_merge($defaults, [
            'child_template'  => 'admin/transactions/index.twig',
            'page_title'      => $_ENV['APP_NAME'] . ' Transactions',
            'recent_sats'     => transactions::getRecent(10, 'sats'),
            'recent_cents'    => transactions::getRecent(10, 'cents'),
            'whales_sats'     => transactions::getWhales(10, 'sats'),
            'whales_cents'    => transactions::getWhales(10, 'cents'),
            'sats_liability'  => transactions::getLiabilities('sats'),
            'cents_liability' => transactions::getLiabilities('cents'),
            'breadcrumbs'     => [
                [
                    'url'   => '/admin',
                    'title' => 'Admin',
                ],
                [
                    'url'   => '/admin/transactions',
                    'title' => 'Transactions',
                ],
            ],
        ]));
    }

    public static function transactions_add($defaults)
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $amount          = $_POST['amount'] ?? null;
            $currency        = $_POST['currency'];
            $user_identifier = $_POST['user_identifier'] ?? null;
            if (! $amount || ! $user_identifier) {
                $_SESSION['error']     = ! $amount ? "Please enter an amount for the transaction." : "Please enter a user email or id for the transaction.";
                $_SESSION['last_post'] = $_POST;
            } else {
                if (strpos($user_identifier, '@') !== false && strpos($user_identifier, '.') !== false) {
                    $user = users::getByEmail($user_identifier);
                } elseif (is_numeric($user_identifier)) {
                    $user = users::getById((int) $user_identifier);
                } else {
                    $_SESSION['error']     = "Invalid user identifier. Please enter a valid email or user ID.";
                    $_SESSION['last_post'] = $_POST;
                }
                if (! $user) {
                    $_SESSION['error']     = "User not found. Please enter a valid email or user ID.";
                    $_SESSION['last_post'] = $_POST;
                } else {
                    if ($_POST['confirm']) {
                        // create the transaction
                        $txid = transactions::add($user['id'], 'CREDIT', $currency == 'cents' ? $amount : 0, $currency == 'sats' ? $amount : 0);
                        header('Location: /transaction/' . $txid);
                        exit;
                    } else {
                        $_SESSION['last_post']            = $_POST;
                        $_SESSION['last_post']['confirm'] = true;
                        $_SESSION['last_post']['email']   = $user['email'];
                        $_SESSION['last_post']['id']      = $user['id'];
                    }
                }
            }
        }
        echo $GLOBALS['twig']->render('lib/pages/index.twig', array_merge($defaults, [
            'child_template' => 'admin/transactions/add.twig',
            'page_title'     => 'Add a Transaction',
            'breadcrumbs'    => [
                [
                    'url'   => '/admin',
                    'title' => 'Admin',
                ],
                [
                    'url'   => '/admin/transactions',
                    'title' => 'Transactions',
                ],
                [
                    'url'   => '/admin/transactions/add',
                    'title' => 'Add',
                ],
            ],
        ]));
    }

    public static function transactions_reset($defaults)
    {
        $_SESSION['last_post'] = null;
        header('Location: /admin/transactions/add');
        exit;
    }
}
