<?php
class AlertRulesController extends Controller
{
    public function indexAction()
    {
        $m = new AlertRules();
        $this->view->setLayout($this->roleLayout());
        $this->view->render('AlertRules/index', [
            'page_title' => 'Alert Rules',
            'rules'      => $m->listAll(),
        ]);
    }

    public function addAction()
    {
        $this->view->setLayout($this->roleLayout());
        $this->view->render('AlertRules/edit', [
            'page_title' => 'Add Alert Rule',
            'rule'       => [],
            'form'       => [],
            'is_new'     => true,
            'fb_msg'     => '',
        ]);
    }

    public function saveAction()
    {
        $this->verifyCsrf();
        if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') { Router::redirect('AlertRules/add'); return; }
        $r = (new AlertRules())->create($_POST, $this->currentUserId());
        if (!$r['ok']) {
            $this->view->setLayout($this->roleLayout());
            $this->view->render('AlertRules/edit', [
                'page_title' => 'Add Alert Rule',
                'rule'       => [],
                'form'       => $_POST,
                'is_new'     => true,
                'fb_msg'     => '<div class="alert alert-danger">' . htmlspecialchars($r['msg']) . '</div>',
            ]);
            return;
        }
        $_SESSION['flash_tenant_msg']  = 'Rule created.';
        $_SESSION['flash_tenant_type'] = 'success';
        Router::redirect('AlertRules');
    }

    public function editAction($id = 0)
    {
        $rule = (new AlertRules())->byId((int)$id);
        if (!$rule) { $this->notFound('Rule'); return; }
        $this->view->setLayout($this->roleLayout());
        $this->view->render('AlertRules/edit', [
            'page_title' => 'Edit · ' . $rule['name'],
            'rule'       => $rule,
            'form'       => $rule,
            'is_new'     => false,
            'fb_msg'     => '',
        ]);
    }

    public function updateAction($id = 0)
    {
        $this->verifyCsrf();
        if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') { Router::redirect('AlertRules/edit/' . (int)$id); return; }
        (new AlertRules())->update((int)$id, $_POST, $this->currentUserId());
        $_SESSION['flash_tenant_msg']  = 'Rule updated.';
        $_SESSION['flash_tenant_type'] = 'success';
        Router::redirect('AlertRules');
    }

    public function toggleAction($id = 0)
    {
        $this->verifyCsrf();
        if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') { Router::redirect('AlertRules'); return; }
        (new AlertRules())->toggleActive((int)$id, $this->currentUserId());
        $_SESSION['flash_tenant_msg']  = 'Rule toggled.';
        $_SESSION['flash_tenant_type'] = 'success';
        Router::redirect('AlertRules');
    }

    public function deleteAction($id = 0)
    {
        $this->verifyCsrf();
        if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') { Router::redirect('AlertRules'); return; }
        (new AlertRules())->softDelete((int)$id, $this->currentUserId());
        $_SESSION['flash_tenant_msg']  = 'Rule removed.';
        $_SESSION['flash_tenant_type'] = 'success';
        Router::redirect('AlertRules');
    }

    private function notFound(string $what): void
    {
        $this->view->setLayout($this->roleLayout());
        $this->view->render('generals/fb_page', [
            'page_title' => 'Not found',
            'fb_msg'     => '<div class="alert alert-danger">' . htmlspecialchars($what) . ' not found.</div>',
        ]);
    }
}
