<?php
session_start();

require_once 'app/Helpers/functions.php';
require_once 'db/config.php';

// Simple Autoloader
spl_autoload_register(function ($class) {
    $prefix = 'App\\';
    $base_dir = __DIR__ . '/app/';
    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        return;
    }
    $relative_class = substr($class, $len);
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
    if (file_exists($file)) {
        require_once $file;
    }
});

use App\Core\Router;

$router = new Router();

// Routes
$router->get('/', 'HomeController@index');
$router->get('/apk/:slug', 'ApkController@detail');
$router->get('/download/:slug', 'ApkController@download');

// Auth
$router->get('/login', 'AuthController@loginForm');
$router->post('/login', 'AuthController@login');
$router->get('/register', 'AuthController@registerForm');
$router->post('/register', 'AuthController@register');
$router->get('/logout', 'AuthController@logout');
$router->get('/profile', 'AuthController@profile');
$router->post('/profile', 'AuthController@updateProfile');

// Admin Routes
$router->get('/admin', 'AdminController@dashboard');
$router->get('/admin/login', 'AdminController@loginForm');
$router->post('/admin/login', 'AdminController@login');
$router->get('/admin/logout', 'AdminController@logout');

// Admin APKs
$router->get('/admin/apks', 'AdminController@apks');
$router->get('/admin/apks/add', 'AdminController@addApkForm');
$router->post('/admin/apks/add', 'AdminController@addApk');
$router->get('/admin/apks/edit/:id', 'AdminController@editApkForm');
$router->post('/admin/apks/edit/:id', 'AdminController@editApk');
$router->get('/admin/apks/delete/:id', 'AdminController@deleteApk');

// Admin Categories
$router->get('/admin/categories', 'AdminController@categories');
$router->post('/admin/categories/add', 'AdminController@addCategory');
$router->get('/admin/categories/delete/:id', 'AdminController@deleteCategory');

// Admin Users
$router->get('/admin/users', 'AdminController@users');
$router->get('/admin/users/delete/:id', 'AdminController@deleteUser');

// Admin Settings
$router->get('/admin/settings', 'AdminController@settingsForm');
$router->post('/admin/settings', 'AdminController@saveSettings');

// Blog
$router->get('/blog', 'BlogController@index');
$router->get('/blog/:slug', 'BlogController@detail');

// Admin Blog
$router->get('/admin/posts', 'AdminController@posts');
$router->get('/admin/posts/add', 'AdminController@addPostForm');
$router->post('/admin/posts/add', 'AdminController@addPost');
$router->get('/admin/posts/edit/:id', 'AdminController@editPostForm');
$router->post('/admin/posts/edit/:id', 'AdminController@editPost');
$router->get('/admin/posts/delete/:id', 'AdminController@deletePost');

// Newsletter
$router->post('/newsletter/subscribe', 'NewsletterController@subscribe');
$router->get('/admin/newsletter', 'AdminController@newsletter');
$router->get('/admin/newsletter/delete/:id', 'AdminController@deleteSubscriber');
$router->get('/admin/newsletter/export', 'AdminController@exportSubscribers');
$router->get('/admin/newsletter/send', 'AdminController@sendNewsletterForm');
$router->post('/admin/newsletter/send', 'AdminController@sendNewsletter');

// Contact
$router->get('/contact', 'ContactController@index');
$router->post('/contact', 'ContactController@send');

// Sitemap
$router->get('/sitemap.xml', 'SitemapController@index');

$router->dispatch();