<?php

namespace App\Controllers;

use App\Core\Auth;
use App\Core\Controller;
use App\Helpers\Validator;
use App\Models\GlobalTopic;

class GlobalTopicController extends Controller
{
    public function index(): void
    {
        Auth::requireLogin();

        $categories = GlobalTopic::findAllGrouped();

        $this->view('global-topics/index', ['categories' => $categories]);
    }

    public function storeCategory(): void
    {
        Auth::requireLogin();

        $validator = new Validator();
        $validator->required('name', $this->input('name'), 'Nazwa kategorii');

        if (!$validator->isValid()) {
            $this->flash('danger', $validator->getFirstError());
            $this->redirect('/global-topics');
            return;
        }

        GlobalTopic::create([
            'parent_id' => null,
            'name' => $this->input('name'),
            'description' => $this->input('description', ''),
        ]);

        $this->flash('success', 'Kategoria została dodana.');
        $this->redirect('/global-topics');
    }

    public function storeSubtopic(string $parentId): void
    {
        Auth::requireLogin();

        $parent = GlobalTopic::find((int) $parentId);
        if (!$parent) {
            $this->flash('danger', 'Kategoria nadrzędna nie znaleziona.');
            $this->redirect('/global-topics');
            return;
        }

        $validator = new Validator();
        $validator->required('name', $this->input('name'), 'Nazwa tematu');

        if (!$validator->isValid()) {
            $this->flash('danger', $validator->getFirstError());
            $this->redirect('/global-topics');
            return;
        }

        GlobalTopic::create([
            'parent_id' => (int) $parentId,
            'name' => $this->input('name'),
            'description' => $this->input('description', ''),
        ]);

        $this->flash('success', 'Temat został dodany.');
        $this->redirect('/global-topics');
    }

    public function update(string $id): void
    {
        Auth::requireLogin();

        $topic = GlobalTopic::find((int) $id);
        if (!$topic) {
            $this->flash('danger', 'Temat nie znaleziony.');
            $this->redirect('/global-topics');
            return;
        }

        GlobalTopic::update((int) $id, [
            'name' => $this->input('name'),
            'description' => $this->input('description', ''),
        ]);

        $this->flash('success', 'Temat został zaktualizowany.');
        $this->redirect('/global-topics');
    }

    public function destroy(string $id): void
    {
        Auth::requireLogin();

        $isAjax = !empty($_SERVER['HTTP_X_REQUESTED_WITH'])
            && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';

        GlobalTopic::delete((int) $id);

        if ($isAjax) {
            $this->json(['success' => true, 'message' => 'Usunięto.']);
            return;
        }

        $this->flash('success', 'Temat został usunięty.');
        $this->redirect('/global-topics');
    }
}
