<?php
class CategoryController extends Controller {

    public function index() {
        $CategoryModel = $this->model('Category');

        // Start session if not started
        if (session_status() == PHP_SESSION_NONE) session_start();

        // ========================
        //   ADD CATEGORY
        // ========================
       if (isset($_POST['add'])) {

    $imageName = null;

    if (!empty($_FILES['cat_image']['name'])) {
        $ext = strtolower(pathinfo($_FILES['cat_image']['name'], PATHINFO_EXTENSION));
        $allowed = ['png','jpg','jpeg','webp'];

        if (in_array($ext, $allowed)) {
            $imageName = time().'_'.$_FILES['cat_image']['name'];
            move_uploaded_file($_FILES['cat_image']['tmp_name'], 'assets/images/category/'.$imageName);
        }
    }

    $CategoryModel->add([
        'cat_title' => $_POST['cat_title'],
        // 'cat_description' => $_POST['cat_description'],
        'cat_image' => $imageName,
        'parent_id' => $_POST['parent_id'] ?: null,
        'status' => $_POST['status']
    ]);

    $_SESSION['message'] = "Category Added!";
    header('Location: '.BASE_URL.'/category');
    exit;
}


        // ========================
        // EDIT CATEGORY
        // ========================
    if (isset($_POST['edit'])) {

    $id = $_POST['id'];
    $imageName = $_POST['old_image'];

    if (!empty($_FILES['cat_image']['name'])) {
        $ext = strtolower(pathinfo($_FILES['cat_image']['name'], PATHINFO_EXTENSION));
        $allowed = ['png','jpg','jpeg','webp'];

        if (in_array($ext, $allowed)) {
            $imageName = time().'_'.$_FILES['cat_image']['name'];
            move_uploaded_file($_FILES['cat_image']['tmp_name'], 'assets/images/category/'.$imageName);
        }
    }

    $CategoryModel->update($id, [
        'cat_title' => $_POST['cat_title'],
        // 'cat_description' => $_POST['cat_description'],
        'cat_image' => $imageName,
        'parent_id' => $_POST['parent_id'] ?: null,
        'status' => $_POST['status']
    ]);

    $_SESSION['message'] = "Updated!";
    header('Location: '.BASE_URL.'/category');
    exit;
}

        // ========================
        // DELETE CATEGORY + PRODUCTS
        // ========================
        if (isset($_GET['delete'])) {
            $id = $_GET['delete'];

            // Load Product Model
            $ProductModel = $this->model('Product');
            $products = $ProductModel->getByCategory($id);

            // Delete products and their images
            foreach ($products as $product) {
                if (!empty($product['pro_image']) && file_exists('assets/images/products/' . $product['pro_image'])) {
                    unlink('assets/images/products/' . $product['pro_image']);
                }
                $ProductModel->delete($product['id']);
            }

            // Delete category image
            $Category = $CategoryModel->get($id);
            if ($Category && !empty($Category['cat_image']) && file_exists('assets/images/category/' . $Category['cat_image'])) {
                unlink('assets/images/category/' . $Category['cat_image']);
            }

            // Delete category
            $CategoryModel->delete($id);

            $_SESSION['message'] = "Category and related products deleted successfully!";
            header('Location: ' . BASE_URL . '/category');
            exit;
        }

        // ========================
        // 🟦 FETCH ALL CATEGORIES
        // ========================
        $data['categorys'] = $CategoryModel->getAll();
        $data['title'] = "Products Category";

        $this->view('templates/header', $data);
        $this->view('category', $data);
        $this->view('templates/footer', $data);
    }
}