<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Models\Community;
use App\Repositories\ResponseRepository;
use Illuminate\Validation\Rule;
use Carbon\Carbon;
class CommunityController extends Controller
{
    public function __construct(ResponseRepository $response){
        $this->response = $response;
        $this->storeRules = [
            'banner_name' => 'required',
            'courses_id' => 'required',
            'courses_name' => 'required',
            'best_selling' => 'required',
            'community_description' => 'required',
        ];
    }

    public function indexCommunity() {
        return $this->response->jsonResponse(false,"Community Details Fetched Successfully", Community::orderBy('community_id', 'desc')->get(), 100);
    }

    public function store(Request $request)
{
    $validate = $this->response->validate($request->all(), $this->storeRules);
    
    if ($validate === true) {
        $request['community_slug'] = $this->response->generateSlug($request['banner_name']);

        $course = new Community();
        $course->banner_name = $request['banner_name'];
        $course->community_slug = $request['community_slug'];
        $course->community_description = $request['community_description'];
        $course->best_selling = $request['best_selling'];
        $course->courses_name = $request['courses_name']; // Add banner_name
        $course->courses_id = $request['courses_id']; // Add courses_id
        $course->active_status = 1;
        $course->created_at = Carbon::now();
        $course->updated_at = Carbon::now();
        $course->current_year = Carbon::now()->year;
        $course->current_month = Carbon::now()->format('F');
        $course->current_dates = Carbon::now()->day;
        $course->save();

        return $this->response->jsonResponse(false, $this->response->message('Community', 'store'), $course, 301);
    } else {
        return $validate;
    }
}
public function show($id) {
    return $this->response->jsonResponse(false, $this->response->message('Community', 'show'), $this->findBanner($id), 102);
}

public function updateCommunity(Request $request) {
    $validate = $this->response->validate($request->all(), $this->storeRules);
    if($validate === true) {
        
        $request['community_slug'] = $this->response->generateSlug($request['banner_name']);
        return $this->response->jsonResponse(false, $this->response->message('Community', 'update'), $this->findBanner($request->id)->update($request->all()), 303);
    } else {
        return $validate;
    }
}

public function destroy($id) {
    $size = $this->findBanner($id);
    if($size) {
        return $this->response->jsonResponse(false, $this->response->message('Community', 'destroy'), $size->delete(), 104);
    }
    return $this->response->jsonResponse(true, 'Community Not Exists', [], 105);
}

public function findBanner($id) {
    return Community::find($id);
}

public function communityBannerSwitch($id) {
    $size = $this->findBanner($id);
    if($size) {
        $value = $size->active_status === 1 ? 0 : 1;
        $msg = $value === 0 ? 'Deactivated': 'Activated';
        return $this->response->jsonResponse(false, 'Community '.$msg.' SuccessFully', $size->update(['active_status' => $value]), 106);
    }
    return $this->response->jsonResponse(true, 'Community Not Exists', [], 107);
}
public function imageUpdateCommunity(Request $request) {
    $getBanner = $this->findBanner($request['banner_id']);
    if($request->hasFile('banner_image')) {
        $file = $request->file('banner_image');
        $name = $this->response->generateSlug($getBanner['banner_name']);
        $uploadUrl = $this->response->cloudinaryImage($request->file('banner_image'), 'Banner', $name);
        return $this->response->jsonResponse(false, $this->response->message('Community', 'image'), $getBanner->update(['banner_image' => $uploadUrl]), 108);
    } else {
        return $this->response->jsonResponse(true, 'Image Banner is too high', [], 109);
    }
}

public function searchCommunity($search){
    if($search === "null") {
        return $this->response->jsonResponse(false, $this->response->message('Community', 'search'), [], 110);
    }
    return $this->response->jsonResponse(false, $this->response->message('Community', 'search'), Community::where('banner_name', 'LIKE', '%'.$search.'%')->get(), 111);
}

}
