<?php

declare(strict_types=1);

use FatBottom\Controllers\AdminController;
use FatBottom\Controllers\AuthController;
use FatBottom\Controllers\StoreController;
use FatBottom\Core\Auth;
use FatBottom\Core\Http;
use FatBottom\Core\Router;
use FatBottom\Services\CartService;
use FatBottom\Services\CustomerService;
use FatBottom\Services\Mailer;
use FatBottom\Services\OrderService;
use FatBottom\Services\OrderStatusService;
use FatBottom\Services\PaymentGateway;
use FatBottom\Services\StatsService;
use FatBottom\Services\TwoFactorService;

$container = require dirname(__DIR__) . '/app/bootstrap.php';
$config = $container['config'];
$db = $container['db'];

$auth = new Auth($db);
$cart = new CartService($db);
$mailer = new Mailer($config);
$payments = new PaymentGateway($config);
$customers = new CustomerService($db);
$orderStatuses = new OrderStatusService($config, $db, $mailer);
$orders = new OrderService($db, $payments, $customers);
$stats = new StatsService($db);
$twoFactor = new TwoFactorService($db, $mailer, $config);

$store = new StoreController($config, $auth, $cart, $db, $orders, $orderStatuses);
$authentication = new AuthController($config, $auth, $cart, $db, $twoFactor);
$admin = new AdminController(
    $config,
    $auth,
    $cart,
    $db,
    $stats,
    $payments,
    $mailer,
    $orderStatuses
);

$path = Http::path();
$user = $auth->user();
$stats->record($path, $user ? (int) $user['id'] : null);

$router = new Router();
$router->get('/', [$store, 'home']);
$router->get('/menu', [$store, 'menu']);
$router->post('/cart/add', [$store, 'addToCart']);
$router->get('/cart', [$store, 'cart']);
$router->post('/cart/update', [$store, 'updateCart']);
$router->post('/cart/remove', [$store, 'removeFromCart']);
$router->get('/checkout', [$store, 'checkout']);
$router->post('/checkout', [$store, 'placeOrder']);
$router->get('/order/complete', [$store, 'orderComplete']);
$router->get('/order/track', [$store, 'orderTracker']);

$router->get('/login', [$authentication, 'loginForm']);
$router->post('/login', [$authentication, 'login']);
$router->get('/register', [$authentication, 'registerForm']);
$router->post('/register', [$authentication, 'register']);
$router->get('/verify', [$authentication, 'verifyForm']);
$router->post('/verify', [$authentication, 'verify']);
$router->post('/logout', [$authentication, 'logout']);
$router->get('/account', [$authentication, 'account']);
$router->post('/account', [$authentication, 'updateAccount']);
$router->get('/unsubscribe', [$authentication, 'unsubscribe']);

$router->get('/admin', [$admin, 'dashboard']);
$router->get('/admin/orders', [$admin, 'orders']);
$router->get('/admin/order', [$admin, 'orderDetail']);
$router->post('/admin/order', [$admin, 'updateOrder']);
$router->post('/admin/order/refund', [$admin, 'refundOrder']);
$router->get('/admin/customers', [$admin, 'customers']);
$router->get('/admin/customer', [$admin, 'customerDetail']);
$router->post('/admin/customer/save', [$admin, 'saveCustomer']);
$router->post('/admin/customer/delete', [$admin, 'deleteCustomer']);
$router->get('/admin/menu', [$admin, 'menu']);
$router->post('/admin/category/save', [$admin, 'saveCategory']);
$router->post('/admin/item/save', [$admin, 'saveItem']);
$router->get('/admin/specials', [$admin, 'specials']);
$router->post('/admin/special/save', [$admin, 'saveSpecial']);
$router->get('/admin/users', [$admin, 'users']);
$router->post('/admin/user/save', [$admin, 'saveUser']);
$router->get('/admin/settings', [$admin, 'settings']);
$router->post('/admin/settings', [$admin, 'saveSettings']);
$router->post('/admin/tax/save', [$admin, 'saveTax']);
$router->get('/admin/newsletters', [$admin, 'newsletters']);
$router->post('/admin/newsletters/send', [$admin, 'sendNewsletter']);
$router->get('/admin/export/menu.pdf', [$admin, 'exportMenuPdf']);
$router->get('/admin/export/orders.csv', [$admin, 'exportOrdersCsv']);
$router->post('/admin/migrate/mysql', [$admin, 'migrateMySql']);

$router->dispatch($_SERVER['REQUEST_METHOD'] ?? 'GET', $path);
