<?php

namespace App\Controllers\Admin;

use App\Models\CategoryModel;

class Categories extends AdminBaseController
{
    public function index()
    {
        if (!$this->checkAuth()) return redirect()->to('/admin/login');

        $model = new CategoryModel();
        $data['categories'] = $model->findAll();
        $data['title'] = 'Manage Categories';

        return view('admin/categories/index', $data);
    }

    public function create()
    {
        if (!$this->checkAuth()) return redirect()->to('/admin/login');

        if ($this->request->getMethod() === 'POST') {
            $model = new CategoryModel();
            
            $slug = strtolower(url_title($this->request->getPost('name')));
            
            $data = [
                'name' => $this->request->getPost('name'),
                'slug' => $slug,
                'description' => $this->request->getPost('description'),
                'status' => $this->request->getPost('status') ?? 1,
            ];

            if ($model->insert($data)) {
                $this->session->setFlashdata('success', 'Category created successfully.');
                return redirect()->to('/admin/categories');
            } else {
                $this->session->setFlashdata('error', 'Failed to create category.');
            }
        }

        $data['title'] = 'Add New Category';
        return view('admin/categories/create', $data);
    }

    public function edit($id)
    {
        if (!$this->checkAuth()) return redirect()->to('/admin/login');

        $model = new CategoryModel();
        $category = $model->find($id);

        if (!$category) {
            $this->session->setFlashdata('error', 'Category not found.');
            return redirect()->to('/admin/categories');
        }

        if ($this->request->getMethod() === 'POST') {
            $slug = strtolower(url_title($this->request->getPost('name')));
            
            $updateData = [
                'name' => $this->request->getPost('name'),
                'slug' => $slug,
                'description' => $this->request->getPost('description'),
                'status' => $this->request->getPost('status') ?? 1,
            ];

            if ($model->update($id, $updateData)) {
                $this->session->setFlashdata('success', 'Category updated successfully.');
                return redirect()->to('/admin/categories');
            } else {
                $this->session->setFlashdata('error', 'Failed to update category.');
            }
        }

        $data['category'] = $category;
        $data['title'] = 'Edit Category: ' . $category['name'];
        return view('admin/categories/edit', $data);
    }

    public function delete($id)
    {
        if (!$this->checkAuth()) return redirect()->to('/admin/login');

        $model = new CategoryModel();
        if ($model->delete($id)) {
            $this->session->setFlashdata('success', 'Category deleted successfully.');
        } else {
            $this->session->setFlashdata('error', 'Failed to delete category.');
        }
        return redirect()->to('/admin/categories');
    }
}
