<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Dashboard\UpdateProfileRequest;
use App\Models\Comment;
use App\Models\Contact;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AdminController extends Controller
{
    //

    public function index()
    {

        $comments = Comment::select(
            DB::raw('MONTH(created_at) as month')
        )->orderBy('created_at', 'asc')->get()->groupBy('month');

        $all_messages = Contact::count();
        $unread_messages = Contact::where('read', 0)->count();
        $read_messages = Contact::where('read', 1)->count();



        return view('admin.index', compact('all_messages', 'unread_messages', 'read_messages', 'comments'));
    }


    // lang
    public function lang($lang)
    {
        // change language
        session()->put('lang', $lang);

        // return
        return redirect()->back();
    }

    // profile 
    public function profile()
    {
        return view('admin.profile.show');
    }

    // profile 
    public function editProfile()
    {
        return view('admin.profile.edit');
    }

    // profile 
    public function updateProfile(UpdateProfileRequest $request)
    {
        $user = auth()->user();

        $data = $request->except('password', 'confirm_password');

        if ($request->password != null) {
            $data['password'] = bcrypt($request->password);
        }

        $user->update($data);

        session()->flash('profile_action_msg', awtTrans('تم التحديث بنجاح'));

        return redirect()->back();
    }
}
