<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Models\SettingManagement\Contact;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;

class ContactController extends Controller{

    public function __construct(){

        $this->middleware('permission:contact-list')
            ->only(["index"]);

        $this->middleware('permission:contact-show')
            ->only(["show"]);

        $this->middleware('permission:contact-create')
            ->only(["store"]);

        $this->middleware('permission:contact-update')
            ->only(['update',"edit"]);

        $this->middleware('permission:contact-delete')
            ->only(['destroy']);

    }

    public function index(): View{
        $contacts = Contact::query()
            ->when(request()->keyword != null, function ($query) {
                $query->search(request()->keyword);
            })
            ->when(request()->active != null, function ($query) {
                $query->whereStatus(request()->active);
            })
            ->orderBy(request()->sortBy ?? 'id', request()->orderBy ?? 'desc')
            ->paginate(request()->limitBy ?? 10);

        return view('backend.contacts.index', compact('contacts'));
    }

    public function show(Contact $contact): View{
        return view('backend.contacts.show', compact('contact'));
    }

    public function destroy(Contact $contact): RedirectResponse{
        $contact->delete();

        return redirect()->route('admin.contacts.index')->with([
            'message' => action_message("delete"),
            'alert-type' => 'success'
        ]);
    }
}
