<?php

namespace App\Http\Controllers;

use App\Enum\OfferStatus;
use App\Enum\UserType;
use App\Models\Artist;
use App\Models\Event;
use App\Models\Offer;
use App\Models\Organizer;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ArtistController extends Controller
{
    //
    public function __construct()
    {
        $this->middleware(['auth', 'verified', 'onboarding', 'role:artist'])->except('show');
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function show($username)
    {
        $user = User::where('username', $username)->firstOrFail();
        $artistProfile = Artist::with('user')
            ->whereHas('user', function ($query) use ($username) {
                return $query->where('username', '=', $username);
            })->firstOrFail();

        if (!Auth::guest()) {
            switch (Auth::user()->type) {
                case UserType::ORGANIZER->value:
                    $hasFinishedOfferForTheArtist = Offer::join('events', 'events.id', '=', 'offers.event_id')
                        ->join('organizers', 'organizers.user_id', '=', 'events.organizer_id')
                        ->join('artists', 'artists.user_id', '=', 'offers.artist_id')
                        ->where('organizers.user_id', '=', Auth::id())
                        ->where('artists.user_id', '=', $user->id)
                        ->where('offers.status', '=', OfferStatus::COMPLETED->value)
                        ->exists();
                    break;
                case UserType::MANAGER->value:
                    $abilityToSeeAndManageOffers = $artistProfile->manager_id === Auth::id();

                    $abilityToSeeAndManageOffers ?
                        $offers = Offer::select('offers.*')
                            ->join('events', 'events.id', '=', 'offers.event_id')
                            ->where('artist_id', $artistProfile->user_id)
                            ->orderBy('offers.status', 'asc')
                            ->orderBy('events.event_date', 'desc')
                            ->paginate(15) : $offers = null;
                    break;
            }
        }

        return view('web.artist.profile')
            ->with('artist', $artistProfile)
            ->with('abilityToPostReview', $hasFinishedOfferForTheArtist ?? false)
            ->with('abilityToSeeAndManageOffers', $abilityToSeeAndManageOffers ?? false)
            ->with('offers', $offers ?? null);
    }

    public function showManager()
    {
        $artist = Artist::with('user')
            ->where('user_id', Auth::id())
            ->firstOrFail();

        return view('web.artist.manager.manager')
            ->with('artist', $artist);
    }

    public function removeManager()
    {
        $artist = Artist::with('user')
            ->where('user_id', Auth::id())
            ->firstOrFail();

        $artist->manager_id = null;

        try {
            $artist->update();
        } catch (Exception $e) {
            return redirect()->back()
                ->with('error', $e->getMessage());
        }

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