<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\StaffProfile;
use App\Http\Requests\StaffProfileValidation;
use App\Traits\SoftDeletesControllerTrait;

use App\Utilities\Paginate;
use App\Utilities\SearchResult;

class StaffProfilesController extends Controller
{
    use SoftDeletesControllerTrait;

    protected function getModel()
    {
        return new StaffProfile();
    }

    public function index()
    {
        if (!auth()->user()?->can('viewAny', StaffProfile::class)) {
            return redirect('/')->with('error', 'You do not have access to view Staff Profiles');
        }

        return view('staff-profiles.index');
    }

    public function store(StaffProfileValidation $request, $id = null)
    {
        if ($id) {
            if (!auth()->user()->can('update', StaffProfile::find($id))) {
                return response()->json(['error' => 'You do not have permission to update that profile'], 403);
            }
        }

        $staff_profile = (new StaffProfile())->saveStaffProfile(requestInput(), $id);
        $staff_profile->refresh();
        $staff_profile->load('tags', 'photos', 'user');

        return response()->json([
            'success' => $staff_profile->full_name.' Saved',
            'staff_profile' => $staff_profile,
        ]);
    }

    public function paginate()
    {
        return (new StaffProfile())->paginateByTags(request());
    }

    public function search()
    {
        request()->validate([
            'terms' => 'required|min:3',
        ]);

        $terms = SearchResult::collectTerms();
        $search_results = StaffProfile::searchResults($terms);

        if ($search_results->count()) {
            $staff_profiles = StaffProfile::whereIn('id', $search_results->pluck('id')->toArray())->get();
        } else {
            $staff_profiles = collect();
        }

        return Paginate::create((new StaffProfile())->loadCollectionAttributes($staff_profiles));
    }

    public function edit($id)
    {
        $staff_profile = StaffProfile::findOrFail($id);

        if (!auth()->user()->can('update', $staff_profile)) {
            if (request()->expectsJson()) {
                return response()->json(['error', 'You do not have permission to update that staff profile'], 403);
            }
            return redirect('/')->with(['error' => 'You do not have permission to update that staff profile']);
        }

        $staff_profile->load('user', 'tags', 'photos');

        if (request()->expectsJson()) {
            return response()->json([
                'staff_profile' => $staff_profile,
            ]);
        }

        return view('staff-profiles.edit', compact('staff_profile'));
    }

    public function publish($id)
    {
        $staff_profile = StaffProfile::findOrFail($id);

        if (!auth()->user()->can('publish', $staff_profile)) {
            return response()->json(['error', 'You do not have permission to publish that staff profile'], 403);
        }

        $staff_profile->publish();

        return response()->json([
            'success' => $staff_profile->full_name.' Published',
            'staff_profile' => $staff_profile,
        ]);
    }

    public function myProfile()
    {
        $staff_profile = StaffProfile::where('user_id', auth()->user()->id)->first();

        if (!$staff_profile) {
            if (auth()->user()->hasRole('sg.staff')) {
                $staff_profile = auth()->user()->createStaffProfile();
            } else {
                return redirect('/hub')->with(['error' => 'You do not have a Bio, please contact marketing']);
            }
        }

        if (!auth()->user()->can('update', $staff_profile)) {
            return redirect('/hub')->with(['error' => 'You do not have a Bio, please contact marketing']);
        }

        $staff_profile->load('user', 'tags', 'photos');
        return view('staff-profiles.edit', compact('staff_profile'));
    }
}
