<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Repositories\SourceRepository;

class SourceController extends Controller
{
    protected $sources;

    public function __construct(
        SourceRepository $sourceRepo
    ) {
        $this->sources = $sourceRepo;
    }

    public function index(Request $request)
    {
        $member = Auth::user();
        return view('back.sources', [
            'mode'    => 'index',
            'handle'  => $member->handle,
            'sources' => $this->sources->getAll(),
            'title'   => "Manage Sources"]);
    }

    public function editSource(Request $request, $id = 0)
    {
        $source = $this->sources->get($id);

        return view('back.sources', [
            'mode'   => 'source-edit',
            'source' => $source,
            'title'  => "Edit Source Info"]);
    }

    public function createSource(Request $Request)
    {
        return view('back.sources', [
            'mode'  => 'source-create',
            'title' => "Enter a new Source"]);
    }

    //actions

    public function sourceEdit(Request $request)
    {
        $token = csrf_token();
        //role check
        $member = Auth::user();
        if ($member->role == 0) {
            $valid = $request->validate([
                'url'    => ['required'],
                'type'   => ['required'],
                'status' => ['required'],
                'format' => ['required'],
            ]);

            if ($valid) {
                $response = $this->sources->edit($request);
                if ($response['status'] == true) {
                    return back()->with('message', $response['message']);
                } else {
                    return back()->withErrors([$response['message']]);
                }
            } else {
                return back()->withErrors(['Misssing some required info, homie.']);
            }
        } else {
            return back()->withErrors(['Nah, you can\'t do this. Wrong permissions.']);
        }
    }

    public function sourceCreate(Request $request)
    {
        $token  = csrf_token();
        $member = Auth::user();
        if ($member->role == 0) {
            $valid = $request->validate([
                'url'    => ['required'],
                'type'   => ['required'],
                'status' => ['required'],
                'format' => ['required'],
            ]);

            if ($valid) {
                $response = $this->sources->add($request);
                if ($response['status'] == true) {
                    return redirect('/den/sources')->with('message', $response['message']);
                } else {
                    return back()->withErrors([$response['message']]);
                }
            } else {
                return back()->withErrors(['Misssing some required info, homie.']);
            }
        } else {
            return back()->withErrors(['Nah, you can\'t do this. Wrong permissions.']);
        }
    }
}
