<?php

namespace Modules\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Modules\User\Models\User;
use Modules\User\Models\Role;

use Modules\Main\Models\City;
use Modules\Post\Models\Post;
use Modules\Post\Models\PostCity;

use App\Http\Controllers\Controller;
use Modules\Post\Models\PostCategory;
use Illuminate\Support\Facades\Storage;

class AdminPostsController extends Controller
{
    public function index()
    {
        $posts = Post::orderBy('id', 'desc');
        if (!auth()->user()->isAdmin() && auth()->user()->isCityManager()) {

        }
        $posts = $posts->get();
        return view('admin::posts.index', [
            'posts' => $posts
        ]);
    }

    public function create()
    {
        return view('admin::posts.create', [
            'categories' => PostCategory::cases()
        ]);
    }

    public function store(Request $request)
    {
        $request['text'] = 'none';
        $validated = $request->validate([
            'name' => 'required',
            'category' => 'required',
            'short_text' => 'max:500',
            'text' => 'required',
            'imageFile' => 'required|mimes:jpg,bmp,png'
        ]);

        if (!Auth::user()->hasRole(Role::SUPER_ADMIN)) {
            if (!Auth::user()->hasRole(Role::CITY_MANAGER)) {
                return back();
            }
            if ($request->has('cities')) {
                $availableCities = GetAvailableCities()->pluck('id')->toArray();
                foreach ($request->cities as $cityId) {
                    if (!in_array($cityId, $availableCities)) {
                        return back();
                    }
                }
            } else {
                return back();
            }
        }
        $path = $request->file('imageFile')->store('posts', ['disk' => 'public']);
        $request['image'] = $path;
        $post = Post::create(
            $request->only(['name', 'short_text', 'text', 'category', 'image', 'post-trixFields'])
        );
        if (array_key_exists('cities', $request->all()) && is_array($request['cities'])) {
            foreach ($request->cities as $cityId) {
                PostCity::create([
                    'post_id' => $post->id,
                    'city_id' => $cityId,
                ]);
            }
        }
        return back()->withSuccess('Новость добавлена успешно');
    }

    public function edit(Post $post)
    {
        return view('admin::posts.edit', [
            'categories' => PostCategory::cases(),
            'post' => $post
        ]);
    }

    public function update(Request $request, Post $post)
    {
        $request['text'] = 'none';

        $validated = $request->validate([
            'name' => 'required',
            'category' => 'required',
            'short_text' => 'max:500',
            'text' => 'required',
            'imageFile' => 'mimes:jpg,bmp,png'
        ]);

        if ($request->file('imageFile')) {
            $path = $request->file('imageFile')->store('posts', ['disk' => 'public']);
            $request['image'] = $path;
        } else {
            $reuqest['image'] = $post->image;
        }

        $post = $post->update(
            $request->only(['name', 'short_text', 'text', 'category', 'image', 'post-trixFields'])
        );
        return back()->withSuccess('Новость обновлена успешно');
    }

    public function delete(Post $post)
    {
        $post->delete();
        return to_route('admin.posts');
    }
}
