<?php

namespace App\Controllers\Frontend\Shopkeeper;

use App\Controllers\FrontendBaseController;
use App\Models\RewarderServiceCategoryModel;
use App\Models\RewarderServiceModel;

class Services extends FrontendBaseController
{
    public $title = 'Services';
    public $menu = 'shopkeeper';
    public $submenu = 'services';

    public function index()
    {
        if (!is_role('Shopkeeper')) {
            return redirect()->to(landing_url());
        }
        $this->permissionCheck('shop_services_manage');

        $shopId = shopkeeper_shop_id();
        $categories = $shopId ? (new RewarderServiceCategoryModel())->where('shop_id', $shopId)->orderBy('id', 'desc')->findAll() : [];

        $services = [];
        if ($shopId) {
            $services = db_connect()->table('rewarder_services s')
                ->select('s.*, c.name as category_name')
                ->join('rewarder_service_categories c', 'c.id = s.category_id', 'left')
                ->where('s.shop_id', $shopId)
                ->orderBy('s.id', 'desc')
                ->get()
                ->getResult();
        }

        return view('frontend/shopkeeper/services/list', compact('categories', 'services', 'shopId'));
    }

    public function saveCategory()
    {
        if (!is_role('Shopkeeper')) {
            return redirect()->to(landing_url());
        }
        $this->permissionCheck('shop_services_manage');
        postAllowed();

        $shopId = shopkeeper_shop_id();
        if (!$shopId) {
            return redirect()->back()->with('notifyError', 'No shop assigned');
        }

        (new RewarderServiceCategoryModel())->create([
            'shop_id' => $shopId,
            'name' => post('name'),
            'status' => 1,
        ]);

        return redirect()->to('shopkeeper/services')->with('notifySuccess', 'Category created');
    }

    public function deleteCategory($id)
    {
        if (!is_role('Shopkeeper')) {
            return redirect()->to(landing_url());
        }
        $this->permissionCheck('shop_services_manage');

        $shopId = shopkeeper_shop_id();
        (new RewarderServiceCategoryModel())->where(['id' => $id, 'shop_id' => $shopId])->delete();
        return redirect()->to('shopkeeper/services')->with('notifySuccess', 'Category deleted');
    }

    public function add()
    {
        if (!is_role('Shopkeeper')) {
            return redirect()->to(landing_url());
        }
        $this->permissionCheck('shop_services_manage');

        $shopId = shopkeeper_shop_id();
        $categories = (new RewarderServiceCategoryModel())->where('shop_id', $shopId)->orderBy('name', 'asc')->findAll();
        return view('frontend/shopkeeper/services/add', compact('categories'));
    }

    public function save()
    {
        if (!is_role('Shopkeeper')) {
            return redirect()->to(landing_url());
        }
        $this->permissionCheck('shop_services_manage');
        postAllowed();

        $shopId = shopkeeper_shop_id();
        (new RewarderServiceModel())->create([
            'shop_id' => $shopId,
            'category_id' => post('category_id') ?: null,
            'name' => post('name'),
            'price' => post('price'),
            'status' => (int) post('status'),
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ]);

        return redirect()->to('shopkeeper/services')->with('notifySuccess', 'Service created');
    }

    public function edit($id)
    {
        if (!is_role('Shopkeeper')) {
            return redirect()->to(landing_url());
        }
        $this->permissionCheck('shop_services_manage');

        $shopId = shopkeeper_shop_id();
        $service = (new RewarderServiceModel())->where(['id' => $id, 'shop_id' => $shopId])->first();
        if (!$service) {
            return redirect()->to('shopkeeper/services')->with('notifyError', 'Service not found');
        }
        $categories = (new RewarderServiceCategoryModel())->where('shop_id', $shopId)->orderBy('name', 'asc')->findAll();
        return view('frontend/shopkeeper/services/edit', compact('service', 'categories'));
    }

    public function update($id)
    {
        if (!is_role('Shopkeeper')) {
            return redirect()->to(landing_url());
        }
        $this->permissionCheck('shop_services_manage');
        postAllowed();

        $shopId = shopkeeper_shop_id();
        (new RewarderServiceModel())->where(['id' => $id, 'shop_id' => $shopId])->set([
            'category_id' => post('category_id') ?: null,
            'name' => post('name'),
            'price' => post('price'),
            'status' => (int) post('status'),
            'updated_at' => date('Y-m-d H:i:s'),
        ])->update();

        return redirect()->to('shopkeeper/services')->with('notifySuccess', 'Service updated');
    }

    public function delete($id)
    {
        if (!is_role('Shopkeeper')) {
            return redirect()->to(landing_url());
        }
        $this->permissionCheck('shop_services_manage');

        $shopId = shopkeeper_shop_id();
        (new RewarderServiceModel())->where(['id' => $id, 'shop_id' => $shopId])->delete();
        return redirect()->to('shopkeeper/services')->with('notifySuccess', 'Service deleted');
    }
}

