<?php

namespace App\Controllers\Frontend\User;

use App\Controllers\FrontendBaseController;
use App\Models\RewarderWalletModel;

class Dashboard extends FrontendBaseController
{
    public $title = 'Customer';
    public $menu = 'user';
    public $submenu = 'dashboard';

    public function index()
    {
        if (is_role('Admin') || is_role('Manager') || is_role('Shopkeeper')) {
            return redirect()->to(landing_url());
        }

        $wallet = (new RewarderWalletModel())->where('user_id', logged('id'))->first();
        $balance = $wallet ? (int) $wallet->points_balance : 0;

        $discounts = db_connect()->table('rewarder_discounts d')
            ->select('d.*, sh.name as shop_name')
            ->join('rewarder_shops sh', 'sh.id = d.shop_id', 'left')
            ->where('d.status', 1)
            ->orderBy('d.id', 'desc')
            ->limit(10)
            ->get()
            ->getResult();

        $orders = db_connect()->table('rewarder_orders o')
            ->select('o.*, sh.name as shop_name')
            ->join('rewarder_shops sh', 'sh.id = o.shop_id', 'left')
            ->where('o.customer_id', logged('id'))
            ->orderBy('o.id', 'desc')
            ->limit(10)
            ->get()
            ->getResult();

        return view('frontend/user/dashboard', compact('balance', 'discounts', 'orders'));
    }
}

