<?php

namespace App\Http\Controllers;

use App\Models\Artist;
use App\Models\Artwork;
use App\Models\Podcast;
use App\Models\Episode;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class ArtistController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = auth()->user();
        $artists = Artist::whereHas('artworks')
            ->withCount('artworks')
            ->orderBy('artworks_count', 'desc')
            ->paginate(100);
        $podcasts = Podcast::where('published', true)->get();
        return view('profile.artists', [
            'user' => $user,
            'pageTitle' => 'Artists',
            'podcasts' => $podcasts,
            'artists' => $artists,
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Artist  $artist
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, $slug)
    {
        $user = auth()->user();
        $artist = Artist::where('slug', $slug)
            ->firstOrFail();
        $artworks = Artwork::where('artist_id', $artist->id)
                ->with('episode')
                ->with('podcast')
                ->orderBy('artworks.created_at', 'desc')
                ->paginate($perPage = 92, $columns = ['*'], $pageName = 'artworks');
        $podcasts = Podcast::where('published', true)->with('episodes')->get();
        return view('profile.artist', [
            'user' => $user,
            'artist' => $artist,
            'artworks' => $artworks,
            'podcasts' => $podcasts,
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Artist  $artist
     * @return \Illuminate\Http\Response
     */
    public function edit(Artist $artist)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Artist  $artist
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Artist $artist)
    {
        $user = auth()->user();
        $artist = $user->artists->first();
        $rules =  [
            'location' => ['string', 'max:255'],
            'alby' => ['email'],
            'website' => ['url'],
            'nasocial' => ['starts_with:@'],
            'name' => ['string', 'unique:artists', 'max:255'],
        ];
        $validator = Validator::make($request->all(), $rules);
        if ($validator->fails()) {
            return back()
                ->withErrors($validator)
                ->withInput();
        }
        $artist->location = $request->location;
        $artist->alby = $request->alby;
        $artist->website = $request->website;
        $artist->nasocial = $request->nasocial;
        $artist->name = $request->name;
        if ($artist->isDirty()) {
            $artist->save();
        }
        return redirect('/profile');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Artist  $artist
     * @return \Illuminate\Http\Response
     */
    public function destroy(Artist $artist)
    {
        //
    }
}