<?php

require_once BASE_PATH
    . '/app/controllers/owner/OwnerBaseController.php';

require_once BASE_PATH
    . '/app/models/PostCategory.php';


class OwnerPostCategoryController
    extends OwnerBaseController
{
    private PostCategory $categoryModel;

    public function __construct()
    {
        $this->requireOwner();

        $this->categoryModel =
            new PostCategory();
    }

    public function index(): void
    {
        $categories =
            $this->categoryModel
                ->getAllForOwner();

        require BASE_PATH
            . '/app/views/owner/posts/categories.php';
    }

    public function store(): void
    {
        $this->requirePost();
        $this->validateCsrf();

        $name =
            trim(
                $_POST['name'] ?? ''
            );

        if ($name === '') {

            $this->flashError(
                'Tên danh mục không được để trống.'
            );

            $this->redirect(
                'owner/post-categories'
            );
        }

        $slug =
            $this->makeUniqueSlug(
                $name
            );

        $this->categoryModel
            ->create([
                'name' => $name,
                'slug' => $slug,
                'is_active' =>
                    isset(
                        $_POST['is_active']
                    )
                    ? 1
                    : 0
            ]);

        $this->flashSuccess(
            'Đã thêm danh mục bài viết.'
        );

        $this->redirect(
            'owner/post-categories'
        );
    }

    public function toggle(): void
    {
        $this->requirePost();
        $this->validateCsrf();

        $id =
            (int)
            ($_POST['category_id'] ?? 0);

        $this->categoryModel
            ->toggle($id);

        $this->flashSuccess(
            'Đã cập nhật danh mục.'
        );

        $this->redirect(
            'owner/post-categories'
        );
    }

    private function makeUniqueSlug(
        string $name
    ): string {

        $text =
            str_replace(
                ['Đ', 'đ'],
                ['D', 'd'],
                $name
            );

        $converted =
            iconv(
                'UTF-8',
                'ASCII//TRANSLIT//IGNORE',
                $text
            );

        if ($converted !== false) {
            $text = $converted;
        }

        $slug =
            strtolower($text);

        $slug =
            preg_replace(
                '/[^a-z0-9]+/',
                '-',
                $slug
            );

        $slug =
            trim(
                $slug,
                '-'
            );

        if ($slug === '') {
            $slug = 'category';
        }

        $base = $slug;
        $number = 2;

        while (
            $this->categoryModel
                ->slugExists($slug)
        ) {

            $slug =
                $base
                . '-'
                . $number;

            $number++;
        }

        return $slug;
    }
}