<?php
namespace Domain\Category;

class CategoryRepository
{
    private $db;

    private const SORT_TYPES = [
        0 => 'data dodania - najstarsze na początku',
        1 => 'data dodania - najnowsze na początku',
        2 => 'data modyfikacji - rosnąco',
        3 => 'data mofyfikacji - malejąco',
        4 => 'ręczne',
        5 => 'alfabetycznie - A - Z',
        6 => 'alfabetycznie - Z - A',
    ];

    private const SORT_ORDER_SQL = [
        0 => 'q1.date_add ASC',
        1 => 'q1.date_add DESC',
        2 => 'q1.date_modify ASC',
        3 => 'q1.date_modify DESC',
        4 => 'q1.o ASC',
        5 => 'q1.name ASC',
        6 => 'q1.name DESC',
    ];

    private const PRODUCTS_PER_PAGE = 12;

    private const LANGUAGE_FALLBACK_NAME_SQL = '(CASE '
        . 'WHEN copy_from IS NULL THEN name '
        . 'WHEN copy_from IS NOT NULL THEN ('
        . 'SELECT name FROM pp_shop_products_langs '
        . 'WHERE lang_id = pspl.copy_from AND product_id = psp.id'
        . ') '
        . 'END) AS name';

    public function __construct($db)
    {
        $this->db = $db;
    }

    /**
     * @return array<int, string>
     */
    public function sortTypes(): array
    {
        return self::SORT_TYPES;
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    public function subcategories($parentId = null): array
    {
        $rows = $this->db->select('pp_shop_categories', ['id'], [
            'parent_id' => $this->toNullableInt($parentId),
            'ORDER' => ['o' => 'ASC'],
        ]);

        if (!is_array($rows)) {
            return [];
        }

        $categories = [];
        foreach ($rows as $row) {
            $categoryId = (int)($row['id'] ?? 0);
            if ($categoryId <= 0) {
                continue;
            }

            $details = $this->categoryDetails($categoryId);
            if (!empty($details)) {
                $categories[] = $details;
            }
        }

        return $categories;
    }

    /**
     * @return array<string, mixed>
     */
    public function categoryDetails($categoryId = ''): array
    {
        $id = (int)$categoryId;
        if ($id <= 0) {
            return $this->defaultCategory();
        }

        $category = $this->db->get('pp_shop_categories', '*', ['id' => $id]);
        if (!is_array($category)) {
            return $this->defaultCategory();
        }

        $category['id'] = (int)($category['id'] ?? 0);
        $category['status'] = $this->toSwitchValue($category['status'] ?? 0);
        $category['view_subcategories'] = $this->toSwitchValue($category['view_subcategories'] ?? 0);
        $category['sort_type'] = (int)($category['sort_type'] ?? 0);
        $category['parent_id'] = $this->toNullableInt($category['parent_id'] ?? null);

        $translations = $this->db->select('pp_shop_categories_langs', '*', ['category_id' => $id]);
        $category['languages'] = [];

        if (is_array($translations)) {
            foreach ($translations as $translation) {
                $langId = (string)($translation['lang_id'] ?? '');
                if ($langId === '') {
                    continue;
                }
                $category['languages'][$langId] = $translation;
            }
        }

        return $category;
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    public function categoryProducts(int $categoryId): array
    {
        if ($categoryId <= 0) {
            return [];
        }

        $rows = $this->db->query(
            'SELECT '
            . 'pspc.product_id, pspc.o, psp.status '
            . 'FROM '
            . 'pp_shop_products_categories AS pspc '
            . 'INNER JOIN pp_shop_products AS psp ON psp.id = pspc.product_id '
            . 'WHERE '
            . 'pspc.category_id = :category_id '
            . 'ORDER BY '
            . 'pspc.o ASC',
            [
                ':category_id' => $categoryId,
            ]
        );

        if (!$rows) {
            return [];
        }

        $products = [];
        foreach ($rows->fetchAll() as $row) {
            $productId = (int)($row['product_id'] ?? 0);
            if ($productId <= 0) {
                continue;
            }

            $products[] = [
                'product_id' => $productId,
                'o' => (int)($row['o'] ?? 0),
                'status' => $this->toSwitchValue($row['status'] ?? 0),
                'name' => $this->productName($productId),
            ];
        }

        return $products;
    }

    public function categoryDelete($categoryId): bool
    {
        $id = (int)$categoryId;
        if ($id <= 0) {
            return false;
        }

        if ((int)$this->db->count('pp_shop_categories', ['parent_id' => $id]) > 0) {
            return false;
        }

        $deleted = (bool)$this->db->delete('pp_shop_categories', ['id' => $id]);
        if ($deleted) {
            $this->refreshCategoryArtifacts();
        }

        return $deleted;
    }

    public function saveCategoriesOrder($categories): bool
    {
        if (!is_array($categories)) {
            return false;
        }

        $this->db->update('pp_shop_categories', ['o' => 0]);

        $position = 0;
        foreach ($categories as $item) {
            $itemId = (int)($item['item_id'] ?? 0);
            if ($itemId <= 0) {
                continue;
            }

            $position++;
            $parentId = $this->toNullableInt($item['parent_id'] ?? null);
            $this->db->update('pp_shop_categories', [
                'o' => $position,
                'parent_id' => $parentId,
            ], [
                'id' => $itemId,
            ]);
        }

        $this->refreshCategoryArtifacts();

        return true;
    }

    public function saveProductOrder($categoryId, $products): bool
    {
        $id = (int)$categoryId;
        if ($id <= 0 || !is_array($products)) {
            return false;
        }

        $this->db->update('pp_shop_products_categories', ['o' => 0], ['category_id' => $id]);

        $position = 0;
        foreach ($products as $item) {
            $productId = (int)($item['item_id'] ?? 0);
            if ($productId <= 0) {
                continue;
            }

            $position++;
            $this->db->update('pp_shop_products_categories', ['o' => $position], [
                'AND' => [
                    'category_id' => $id,
                    'product_id' => $productId,
                ],
            ]);
        }

        return true;
    }

    /**
     * @param array<string, mixed> $data
     */
    public function save(array $data): ?int
    {
        $categoryId = (int)($data['id'] ?? 0);
        $parentId = $this->toNullableInt($data['parent_id'] ?? null);

        $row = [
            'status' => $this->toSwitchValue($data['status'] ?? 0),
            'parent_id' => $parentId,
            'sort_type' => (int)($data['sort_type'] ?? 0),
            'view_subcategories' => $this->toSwitchValue($data['view_subcategories'] ?? 0),
        ];

        if ($categoryId <= 0) {
            $row['o'] = $this->maxOrder() + 1;
            $this->db->insert('pp_shop_categories', $row);
            $categoryId = (int)$this->db->id();

            if ($categoryId <= 0) {
                return null;
            }
        } else {
            $this->db->update('pp_shop_categories', $row, ['id' => $categoryId]);
        }

        $title = is_array($data['title'] ?? null) ? $data['title'] : [];
        $text = is_array($data['text'] ?? null) ? $data['text'] : [];
        $textHidden = is_array($data['text_hidden'] ?? null) ? $data['text_hidden'] : [];
        $seoLink = is_array($data['seo_link'] ?? null) ? $data['seo_link'] : [];
        $metaTitle = is_array($data['meta_title'] ?? null) ? $data['meta_title'] : [];
        $metaDescription = is_array($data['meta_description'] ?? null) ? $data['meta_description'] : [];
        $metaKeywords = is_array($data['meta_keywords'] ?? null) ? $data['meta_keywords'] : [];
        $noindex = is_array($data['noindex'] ?? null) ? $data['noindex'] : [];
        $categoryTitle = is_array($data['category_title'] ?? null) ? $data['category_title'] : [];
        $additionalText = is_array($data['additional_text'] ?? null) ? $data['additional_text'] : [];

        foreach ($title as $langId => $langTitle) {
            $translationData = [
                'lang_id' => (string)$langId,
                'title' => $this->toNullableString($langTitle),
                'text' => $this->toNullableString($text[$langId] ?? null),
                'text_hidden' => $this->toNullableString($textHidden[$langId] ?? null),
                'meta_description' => $this->toNullableString($metaDescription[$langId] ?? null),
                'meta_keywords' => $this->toNullableString($metaKeywords[$langId] ?? null),
                'meta_title' => $this->toNullableString($metaTitle[$langId] ?? null),
                'seo_link' => $this->normalizeSeoLink($seoLink[$langId] ?? null),
                'noindex' => (int)($noindex[$langId] ?? 0),
                'category_title' => $this->toNullableString($categoryTitle[$langId] ?? null),
                'additional_text' => $this->toNullableString($additionalText[$langId] ?? null),
            ];

            $translationId = $this->db->get('pp_shop_categories_langs', 'id', [
                'AND' => [
                    'category_id' => $categoryId,
                    'lang_id' => (string)$langId,
                ],
            ]);

            if ($translationId) {
                $this->db->update('pp_shop_categories_langs', $translationData, ['id' => $translationId]);
                continue;
            }

            $this->db->insert('pp_shop_categories_langs', array_merge($translationData, [
                'category_id' => $categoryId,
            ]));
        }

        $this->refreshCategoryArtifacts();

        return $categoryId;
    }

    public function categoryTitle(int $categoryId): string
    {
        if ($categoryId <= 0) {
            return '';
        }

        $title = $this->db->select('pp_shop_categories_langs', [
            '[><]pp_langs' => ['lang_id' => 'id'],
        ], 'title', [
            'AND' => [
                'category_id' => $categoryId,
                'title[!]' => '',
            ],
            'ORDER' => ['o' => 'ASC'],
            'LIMIT' => 1,
        ]);

        if (!is_array($title) || !isset($title[0])) {
            return '';
        }

        return (string)$title[0];
    }

    // ===== Frontend methods =====

    public function getCategorySort(int $categoryId): int
    {
        if ($categoryId <= 0) {
            return 0;
        }

        $cacheHandler = new \Shared\Cache\CacheHandler();
        $cacheKey = "get_category_sort:$categoryId";
        $objectData = $cacheHandler->get($cacheKey);

        if ($objectData) {
            return (int)unserialize($objectData);
        }

        $sortType = (int)$this->db->get('pp_shop_categories', 'sort_type', ['id' => $categoryId]);
        $cacheHandler->set($cacheKey, $sortType);

        return $sortType;
    }

    public function categoryName(int $categoryId, string $langId): string
    {
        if ($categoryId <= 0 || $langId === '') {
            return '';
        }

        $cacheHandler = new \Shared\Cache\CacheHandler();
        $cacheKey = "category_name:{$langId}:{$categoryId}";
        $objectData = $cacheHandler->get($cacheKey);

        if ($objectData) {
            return (string)unserialize($objectData);
        }

        $name = $this->db->get('pp_shop_categories_langs', 'title', [
            'AND' => [
                'category_id' => $categoryId,
                'lang_id' => $langId,
            ],
        ]);

        $cacheHandler->set($cacheKey, $name);

        return (string)$name;
    }

    public function categoryUrl(int $categoryId, string $langId): string
    {
        if ($categoryId <= 0) {
            return '#';
        }

        $category = $this->frontCategoryDetails($categoryId, $langId);
        if (empty($category)) {
            return '#';
        }

        $url = !empty($category['language']['seo_link'])
            ? '/' . $category['language']['seo_link']
            : '/k-' . $category['id'] . '-' . \Shared\Helpers\Helpers::seo($category['language']['title'] ?? '');

        $currentLang = \Shared\Helpers\Helpers::get_session('current-lang');
        $defaultLang = (new \Domain\Languages\LanguagesRepository($this->db))->defaultLanguage();

        if ($currentLang != $defaultLang && $url !== '#') {
            $url = '/' . $currentLang . $url;
        }

        return $url;
    }

    /**
     * @return array<string, mixed>
     */
    public function frontCategoryDetails(int $categoryId, string $langId): array
    {
        if ($categoryId <= 0) {
            return [];
        }

        $cacheHandler = new \Shared\Cache\CacheHandler();
        $cacheKey = "front_category_details:{$categoryId}:{$langId}";
        $objectData = $cacheHandler->get($cacheKey);

        if ($objectData) {
            return unserialize($objectData);
        }

        $category = $this->db->get('pp_shop_categories', '*', ['id' => $categoryId]);
        if (!is_array($category)) {
            return [];
        }

        $category['language'] = $this->db->get('pp_shop_categories_langs', '*', [
            'AND' => [
                'category_id' => $categoryId,
                'lang_id' => $langId,
            ],
        ]);

        $cacheHandler->set($cacheKey, $category);

        return $category;
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    public function categoriesTree(string $langId, ?int $parentId = null): array
    {
        $cacheHandler = new \Shared\Cache\CacheHandler();
        $cacheKey = "categories_tree:{$langId}:{$parentId}";
        $objectData = $cacheHandler->get($cacheKey);

        if ($objectData) {
            return unserialize($objectData);
        }

        $categories = [];
        $results = $this->db->select('pp_shop_categories', 'id', [
            'parent_id' => $parentId,
            'ORDER' => ['o' => 'ASC'],
        ]);

        if (is_array($results)) {
            foreach ($results as $row) {
                $category = $this->frontCategoryDetails((int)$row, $langId);
                $category['categories'] = $this->categoriesTree($langId, (int)$row);
                $categories[] = $category;
            }
        }

        $cacheHandler->set($cacheKey, $categories);

        return $categories;
    }

    /**
     * @return array<int, mixed>
     */
    public function blogCategoryProducts(int $categoryId, string $langId, int $limit): array
    {
        if ($categoryId <= 0 || $langId === '' || $limit <= 0) {
            return [];
        }

        $cacheHandler = new \Shared\Cache\CacheHandler();
        $cacheKey = "blog_category_products:{$categoryId}:{$langId}:{$limit}";
        $objectData = $cacheHandler->get($cacheKey);

        if ($objectData) {
            return unserialize($objectData);
        }

        $rows = $this->db->query(
            'SELECT * FROM ('
            . 'SELECT '
                . 'psp.id, date_modify, date_add, o, '
                . self::LANGUAGE_FALLBACK_NAME_SQL . ' '
            . 'FROM '
                . 'pp_shop_products_categories AS pspc '
                . 'INNER JOIN pp_shop_products AS psp ON psp.id = pspc.product_id '
                . 'INNER JOIN pp_shop_products_langs AS pspl ON pspl.product_id = pspc.product_id '
            . 'WHERE '
                . 'status = 1 AND category_id = :category_id AND lang_id = :lang_id'
            . ') AS q1 '
            . 'WHERE '
                . 'q1.name IS NOT NULL '
            . 'ORDER BY '
                . 'RAND() '
            . 'LIMIT ' . (int)$limit,
            [
                ':category_id' => $categoryId,
                ':lang_id' => $langId,
            ]
        );

        $output = [];
        if ($rows) {
            foreach ($rows->fetchAll() as $row) {
                $output[] = $row['id'];
            }
        }

        $cacheHandler->set($cacheKey, $output);

        return $output;
    }

    public function categoryProductsCount(int $categoryId, string $langId): int
    {
        if ($categoryId <= 0 || $langId === '') {
            return 0;
        }

        $cacheHandler = new \Shared\Cache\CacheHandler();
        $cacheKey = "category_products_count:{$categoryId}:{$langId}";
        $objectData = $cacheHandler->get($cacheKey);

        if ($objectData) {
            return (int)unserialize($objectData);
        }

        $rows = $this->db->query(
            'SELECT COUNT(0) FROM ('
            . 'SELECT '
                . 'psp.id, '
                . self::LANGUAGE_FALLBACK_NAME_SQL . ' '
            . 'FROM '
                . 'pp_shop_products_categories AS pspc '
                . 'INNER JOIN pp_shop_products AS psp ON psp.id = pspc.product_id '
                . 'INNER JOIN pp_shop_products_langs AS pspl ON pspl.product_id = pspc.product_id '
            . 'WHERE '
                . 'status = 1 AND category_id = :category_id AND lang_id = :lang_id'
            . ') AS q1 '
            . 'WHERE '
                . 'q1.name IS NOT NULL',
            [
                ':category_id' => $categoryId,
                ':lang_id' => $langId,
            ]
        );

        $productsCount = 0;
        if ($rows) {
            $result = $rows->fetchAll();
            $productsCount = (int)($result[0][0] ?? 0);
        }

        $cacheHandler->set($cacheKey, $productsCount);

        return $productsCount;
    }

    /**
     * @return array<int, mixed>
     */
    public function productsId(int $categoryId, int $sortType, string $langId, int $productsLimit, int $from): array
    {
        if ($categoryId <= 0 || $langId === '') {
            return [];
        }

        $order = self::SORT_ORDER_SQL[$sortType] ?? self::SORT_ORDER_SQL[0];
        $today = date('Y-m-d');

        $cacheHandler = new \Shared\Cache\CacheHandler();
        $cacheKey = "products_id:{$categoryId}:{$sortType}:{$langId}:{$productsLimit}:{$from}";
        $objectData = $cacheHandler->get($cacheKey);

        if ($objectData) {
            return unserialize($objectData);
        }

        $rows = $this->db->query(
            'SELECT * FROM ('
            . 'SELECT '
                . 'psp.id, date_modify, date_add, o, '
                . self::LANGUAGE_FALLBACK_NAME_SQL . ', '
                . '(CASE '
                    . 'WHEN new_to_date >= :today THEN new_to_date '
                    . 'WHEN new_to_date < :today2 THEN null '
                . 'END) AS new_to_date, '
                . '(CASE WHEN (quantity + (SELECT IFNULL(SUM(quantity),0) FROM pp_shop_products WHERE parent_id = psp.id)) > 0 THEN 1 ELSE 0 END) AS total_quantity '
            . 'FROM '
                . 'pp_shop_products_categories AS pspc '
                . 'INNER JOIN pp_shop_products AS psp ON psp.id = pspc.product_id '
                . 'INNER JOIN pp_shop_products_langs AS pspl ON pspl.product_id = pspc.product_id '
            . 'WHERE '
                . 'status = 1 AND category_id = :category_id AND lang_id = :lang_id'
            . ') AS q1 '
            . 'WHERE '
                . 'q1.name IS NOT NULL '
            . 'ORDER BY '
                . $order . ' '
            . 'LIMIT ' . (int)$from . ',' . (int)$productsLimit,
            [
                ':category_id' => $categoryId,
                ':lang_id' => $langId,
                ':today' => $today,
                ':today2' => $today,
            ]
        );

        $output = [];
        if ($rows) {
            foreach ($rows->fetchAll() as $row) {
                $output[] = $row['id'];
            }
        }

        $cacheHandler->set($cacheKey, $output);

        return $output;
    }

    /**
     * @return array{products: array, ls: int}
     */
    public function paginatedCategoryProducts(int $categoryId, int $sortType, string $langId, int $page): array
    {
        $count = $this->categoryProductsCount($categoryId, $langId);
        if ($count <= 0) {
            return ['products' => [], 'ls' => 0];
        }

        $totalPages = (int)ceil($count / self::PRODUCTS_PER_PAGE);

        if ($page < 1) {
            $page = 1;
        } elseif ($page > $totalPages) {
            $page = $totalPages;
        }

        $from = self::PRODUCTS_PER_PAGE * ($page - 1);

        return [
            'products' => $this->productsId($categoryId, $sortType, $langId, self::PRODUCTS_PER_PAGE, $from),
            'ls' => $totalPages,
        ];
    }

    private function maxOrder(): int
    {
        return (int)$this->db->max('pp_shop_categories', 'o');
    }

    private function refreshCategoryArtifacts(): void
    {
        \Shared\Helpers\Helpers::htacces();
        \Shared\Helpers\Helpers::delete_dir('../temp/');
    }

    private function normalizeSeoLink($value): ?string
    {
        $seo = \Shared\Helpers\Helpers::seo((string)$value);
        $seo = trim((string)$seo);

        return $seo !== '' ? $seo : null;
    }

    private function toNullableString($value): ?string
    {
        $text = trim((string)$value);
        return $text === '' ? null : $text;
    }

    private function toSwitchValue($value): int
    {
        if (is_bool($value)) {
            return $value ? 1 : 0;
        }

        if (is_numeric($value)) {
            return ((int)$value) === 1 ? 1 : 0;
        }

        if (is_string($value)) {
            $normalized = strtolower(trim($value));
            return in_array($normalized, ['1', 'on', 'true', 'yes'], true) ? 1 : 0;
        }

        return 0;
    }

    private function toNullableInt($value): ?int
    {
        if ($value === null || $value === '' || (int)$value <= 0) {
            return null;
        }

        return (int)$value;
    }

    private function defaultCategory(): array
    {
        return [
            'id' => 0,
            'status' => 1,
            'parent_id' => null,
            'sort_type' => 0,
            'view_subcategories' => 0,
            'languages' => [],
        ];
    }

    private function productName(int $productId): string
    {
        if ($productId <= 0) {
            return '';
        }

        $defaultLang = $this->db->get('pp_langs', 'id', ['start' => 1]);
        if (!$defaultLang) {
            $defaultLang = 'pl';
        }

        $name = $this->db->get('pp_shop_products_langs', 'name', [
            'AND' => [
                'product_id' => $productId,
                'lang_id' => (string)$defaultLang,
            ],
        ]);

        if ($name !== false && $name !== null && trim((string)$name) !== '') {
            return (string)$name;
        }

        $fallback = $this->db->get('pp_shop_products_langs', 'name', [
            'AND' => [
                'product_id' => $productId,
                'name[!]' => '',
            ],
            'LIMIT' => 1,
        ]);

        return $fallback ? (string)$fallback : '';
    }

    public function getCategoryProductIds(int $categoryId): array
    {
        if ($categoryId <= 0) {
            return [];
        }

        $result = $this->db->select('pp_shop_products_categories', 'product_id', ['category_id' => $categoryId]);

        return is_array($result) ? $result : [];
    }

}
