<?php

use WoWPress\Models\Log;
use WoWPress\Models\Raid;
use WoWPress\Models\SKS;
use WoWPress\Models\User;

add_filter('generate_rewrite_rules', function ($wp_rewrite) {
    $wp_rewrite->rules = array_merge(
        ['roster/?$' => 'index.php?wowpress_page=roster'],
        ['raids/?$' => 'index.php?wowpress_page=raids&year=' . date('Y') . "&month=" . date('m')],
        ['raids/(\d+)/(\d+)?$' => 'index.php?wowpress_page=raids&year=$matches[1]&month=$matches[2]'],
        ['raid/(\d+)/?$' => 'index.php?wowpress_page=raid&raid_id=$matches[1]'],
        ['request/?$' => 'index.php?wowpress_page=form_request'],
        ['sks/?$' => 'index.php?wowpress_page=sks'],
        ['sks/([A-Za-z-0-9 ]+)?$' => 'index.php?wowpress_page=sks&list_name=$matches[1]'],
        ['logs/?$' => 'index.php?wowpress_page=logs'],
        ['db/?$' => 'index.php?wowpress_page=db'],
        ['kummerkasten/?$' => 'index.php?wowpress_page=complaints'],
        ['kummerkasten/(\d+)/?$' => 'index.php?wowpress_page=complaints&complaint_id=$matches[1]'],

        $wp_rewrite->rules
    );
});

add_filter('query_vars', function ($query_vars) {
    $query_vars[] = 'wowpress_page';
    $query_vars[] = 'year';
    $query_vars[] = 'month';
    $query_vars[] = 'raid_id';
    $query_vars[] = 'list_name';
    $query_vars[] = 'complaint_id';


    return $query_vars;
});

add_action('template_redirect', function () {
    $page = get_query_var('wowpress_page');

    setGlobalUser();


    switch ($page) {
        case 'db':
            isAllowed('wowpress_edit_site');
            wowpress_database();
            header('Location: /');
            die;
            break;
        case 'complaints':
            isAllowed('wowpress_view_complaints');
            $GLOBALS['wowpress']['page_title'] = "Kummerkasten";
            include plugin_dir_path(__FILE__) . '../pages/complaints.php';
            die;
            break;
        case 'roster':
            $GLOBALS['wowpress']['page_title'] = "Gildenverzeichnis";
            include plugin_dir_path(__FILE__) . '../pages/roster.php';
            die;
            break;
        case 'sks':
            isAllowed('wowpress_view_raids');
            $GLOBALS['wowpress']['page_title'] = "SKS-Liste";
            $GLOBALS['wowpress']['liste'] = get_query_var('list_name', null);

            include plugin_dir_path(__FILE__) . '../pages/sks.php';
            die;
            break;
        case 'logs':
            
            isAllowed('wowpress_edit_raids');
            $GLOBALS['wowpress']['page_title'] = "Logs";
            echo "<div style='display:flex;flex-direction:column;gap:2em;'>";
            echo '<hr style="width:100%">';
            foreach(Log::all() as $log){
                ?>
                <div style="display:flex;flex-direction:column;gap:1em;">
                    <div>Message: <?=$log->message?></div>
                    <div>Action: <?=$log->action?></div>
                    <div>Logged: <?=format_date($log->created_at,"dd.MM.YY HH:mm")?></div>
                </div>
                <hr style="width:100%">
                <?php
            }
            echo "<div>";
            die;
            break;
        case 'raids':
            # isAllowed('wowpress_view_raids');
            $GLOBALS['wowpress']['page_title'] = "Raids";
            $GLOBALS['wowpress']['raids']['year'] = get_query_var('year');
            $GLOBALS['wowpress']['raids']['month'] = get_query_var('month');

            include plugin_dir_path(__FILE__) . '../pages/raids.php';
            die;
            break;
        case 'raid':
            isAllowed('wowpress_view_raids');
            $raid = Raid::find(get_query_var('raid_id'));
            $GLOBALS['wowpress']['raid'] = $raid;
            $GLOBALS['wowpress']['page_title'] = $raid->title;

            include plugin_dir_path(__FILE__) . '../pages/single_raid.php';
            die;
            break;
        case 'form_request':
            include plugin_dir_path(__FILE__) . '../request.php';
            die;
        default:
            // DO NOTHING
            break;
    }
});
