<?php

namespace App\Http\Controllers;

use App\Helpers\Helper;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class UserProfileController extends Controller
{
    use Helper;
    public function __construct()
    {
        $this->model = new User();
    }
    public function index()
    {
        $user = auth()->user();
        $user->load('roles', 'default_sender');
        $user->role_name = $user->roles ? $user->roles->name : ($user->is_superadmin == 1 ? 'Super Administrator' : 'Standard User');
        $user->active_package_info = \App\Services\PackageActivationService::getUserActivePackage($user->id);
        $user->account_balance = function_exists('showBalance') ? showBalance($user->id) : 0;
        
        $data = $this->model->where('id', $user->id)->with('roles', 'default_sender')->orderBy('id','desc')->paginate(15);
        // Attach rich attributes to paginated record
        if ($data->count() > 0) {
            $data[0]->role_name = $user->role_name;
            $data[0]->active_package_info = $user->active_package_info;
            $data[0]->account_balance = $user->account_balance;
        }
        return returnData(2000, $data);
    }


    public function create()
    {
        //
    }


    public function store(Request $request)
    {
        try{
            $user = Auth::user();
            if(auth()->user()){
                $user->name = $request->input('name');
                $user->company_name = $request->input('company_name');
                $user->address = $request->input('address');
                $user->nid = $request->input('nid');
                $user->contact_number = $request->input('contact_number');
                $user->email = $request->input('email');
                $user->layout = $request->input('layout');
                if ($request->filled('theme_mode')) {
                    $user->theme_mode = $request->input('theme_mode');
                }
                $user->save();
                if($request->input('password')){
                    if (!Hash::check($request->old_password, $user->password)) {
                        return returnData(3000, null, 'Old password is incorrect');
                    }
                    $user->password = Hash::make($request->password);
                }
                $user->save();
            }
            return returnData(2000, null, 'Successfully Updated');
        }catch (\Exception $exception){
            return returnData(5000, $exception->getMessage(), $exception->getMessage());
        }
    }

    public function updateThemeMode(Request $request)
    {
        try {
            $themeMode = $request->input('theme_mode', 'light');
            if (!in_array($themeMode, ['light', 'dark', 'system'], true)) {
                $themeMode = 'light';
            }

            $user = Auth::user();
            if ($user) {
                $user->theme_mode = $themeMode;
                $user->save();
            }

            return returnData(2000, ['theme_mode' => $themeMode], 'Theme mode updated successfully');
        } catch (\Exception $exception) {
            return returnData(5000, $exception->getMessage(), $exception->getMessage());
        }
    }

    public function show($id)
    {
        //
    }


    public function edit($id)
    {
        //
    }


    public function update(Request $request, $id)
    {

    }


    public function destroy($id)
    {
        //
    }
}
