<?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',
    ];

    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];
    }

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

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

    private function normalizeSeoLink($value): ?string
    {
        if (!class_exists('\\S')) {
            return $this->toNullableString($value);
        }

        $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 : '';
    }
}
