<?php

namespace App\Models;

use App\Core\Model;

class Category extends Model
{
    protected string $table = 'categories';
    protected string $primaryKey = 'category_id';

    public function getActive(): array
    {
        $sql = "SELECT * FROM {$this->table} 
                WHERE is_active = TRUE 
                ORDER BY sort_order, name";
        return $this->query($sql);
    }

    public function getParent(): array
    {
        $sql = "SELECT * FROM {$this->table} 
                WHERE parent_id IS NULL AND is_active = TRUE 
                ORDER BY sort_order, name";
        return $this->query($sql);
    }

    public function getChildren(int $parentId): array
    {
        $sql = "SELECT * FROM {$this->table} 
                WHERE parent_id = ? AND is_active = TRUE 
                ORDER BY sort_order, name";
        return $this->query($sql, [$parentId]);
    }

    public function findBySlug(string $slug): ?array
    {
        return $this->findWhere(['slug' => $slug]);
    }

    public function getAllWithProductCount(): array
    {
        $sql = "SELECT c1.*, c2.name as parent_name,
                (SELECT COUNT(*) FROM products p WHERE p.category_id = c1.category_id) as product_count
                FROM {$this->table} c1
                LEFT JOIN {$this->table} c2 ON c1.parent_id = c2.category_id
                ORDER BY c1.sort_order, c1.name";
        return $this->query($sql);
    }

    public function createCategory(array $data): ?int
    {
        $slug = $this->generateSlug($data['name']);
        
        return $this->create([
            'name' => $data['name'],
            'slug' => $slug,
            'parent_id' => $data['parent_id'] ?? null,
            'description' => $data['description'] ?? null,
            'sort_order' => $data['sort_order'] ?? 0,
            'is_active' => $data['is_active'] ?? true
        ]);
    }

    public function updateCategory(int $id, array $data): bool
    {
        $updateData = [
            'name' => $data['name'],
            'slug' => $this->generateSlug($data['name']),
            'parent_id' => $data['parent_id'] ?? null,
            'description' => $data['description'] ?? null,
            'sort_order' => $data['sort_order'] ?? 0,
            'is_active' => $data['is_active'] ?? true,
            'updated_at' => date('Y-m-d H:i:s')
        ];
        
        return $this->update($id, $updateData);
    }

    public function safeDelete(int $id): array
    {
        $sql = "SELECT COUNT(*) as cnt FROM products WHERE category_id = ?";
        $result = $this->queryOne($sql, [$id]);
        $productCount = (int) $result['cnt'];

        $sql = "SELECT COUNT(*) as cnt FROM {$this->table} WHERE parent_id = ?";
        $result = $this->queryOne($sql, [$id]);
        $childCount = (int) $result['cnt'];

        if ($productCount > 0 || $childCount > 0) {
            $this->update($id, ['is_active' => false]);
            return [
                'deleted' => false,
                'hidden' => true,
                'reason' => $productCount > 0 ? 'has_products' : 'has_children'
            ];
        }

        $this->delete($id);
        return ['deleted' => true, 'hidden' => false];
    }

    private function generateSlug(string $name): string
    {
        $slug = mb_strtolower($name);
        
        $transliteration = [
            'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
            'е' => 'e', 'ё' => 'yo', 'ж' => 'zh', 'з' => 'z', 'и' => 'i',
            'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n',
            'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't',
            'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'ts', 'ч' => 'ch',
            'ш' => 'sh', 'щ' => 'sch', 'ъ' => '', 'ы' => 'y', 'ь' => '',
            'э' => 'e', 'ю' => 'yu', 'я' => 'ya'
        ];
        
        $slug = strtr($slug, $transliteration);
        $slug = preg_replace('/[^a-z0-9]+/', '-', $slug);
        $slug = trim($slug, '-');
        
        return $slug;
    }
}
