<?php

namespace App\Http\Controllers\Web;

use App\Category;
use App\Course;
use App\Helpers\ImageHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\SubscribeRequest;
use App\Subject;
use App\Subscription;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CategoryController extends Controller
{
    public function show(Request $request, Category $category)
    {
        $title = __('courses.head');
//        $categories = $category->join('category_descriptions AS cd', 'cd.category_id', 'categories.id')
//            ->where('cd.language_id', currentLanguage()->id)
//            ->select(['cd.name', 'categories.id'])
//            ->cursor();

        $courses = Course::where('courses.category_id', $category->id)->join('category_descriptions AS cd', 'cd.category_id', 'courses.category_id')
            ->where('cd.language_id', currentLanguage()->id)->select(['courses.*', 'cd.name AS category_name', 'cd.category_id'])->cursor();

        if ($request->search){
            $title = __('main.search_course');
            $courses = Course::where('courses.name', 'LIKE', '%'.$request->search.'%')->join('category_descriptions AS cd', 'cd.category_id', 'courses.category_id')
                ->where('cd.language_id', currentLanguage()->id)->select(['courses.*', 'cd.name AS category_name', 'cd.category_id'])->cursor();
        }

        return view('web.categories.index', compact('courses', 'title'));
    }

    public function courses(Course $course)
    {
        $title = __('courses.head');
        $category = $course->category->descriptions->where('language_id', currentLanguage()->id)->first();
        return view('web.courses.index', compact('course', 'title', 'category'));
    }

    public function courseSubscription(Course $course)
    {
        return redirect()->route('subscription', $course);
    }

    public function subscription(Course $course)
    {
        $title = __('courses.subscription');
        return view('web.courses.subscribe_form', compact('course', 'title'));
    }

    public function doSubscription(SubscribeRequest $request)
    {
        $course = Course::find($request->course_id);

        $isCourseSubscribe = $course->users()->first();

        if ($isCourseSubscribe){
            return back()->withInput($request->all())->withMessage(__('courses.subscribe_before'));
        }

        $data = $request->all();
        $data['trans_document'] = ImageHelper::upload($request->trans_document);

        if ($request->terms){
            $data['terms'] = 1;
        }

        Subscription::create($data);

        $course->users()->sync(Auth::id());

        return back()->withMessage(__('courses.subscribe_success'));

    }

    public function coursesSearch(Request $request)
    {
        $title = __('categories.search');
        $courses = Course::where('courses.name', 'LIKE', '%'.$request->search.'%')->join('category_descriptions AS cd', 'cd.category_id', 'courses.category_id')
            ->where('cd.language_id', currentLanguage()->id)->select(['courses.*', 'cd.name AS category_name', 'cd.category_id'])->get();

        return view('web.categories.index', compact('courses', 'title'));

    }
}
