<?php

namespace App\Http\Controllers\Dashboard\Status;

use App\Http\Controllers\Controller as ExController;
use Illuminate\Http\Request;
use App\Models\Status;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class StatusController extends ExController
{
    public function index(Request $request)
    {
        $statuses = Status::latest('id')->paginate(30);

        return view('dashboard.statuses.index', compact('statuses'));
    }

    public function create()
    {
        return view('dashboard.statuses.create');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'slug' => 'required|string|unique:statuses',
            'font_color' => 'required|string',
            'bg_color' => 'required|string',
        ]);

        Status::create([
            'slug' => $request->slug,
            'font_color' => $request->font_color,
            'bg_color' => $request->bg_color,
        ]);

        $this->info(trans('admin.messages.created'));
        return redirect()->route('dashboard.statuses.index');
    }

    public function edit($statusId)
    {
        $status = Status::findOrFail($statusId);
        return view('dashboard.statuses.edit', compact('status'));
    }

    public function update(Request $request, $statusId)
    {
        $status = Status::findOrFail($statusId);

        $this->validate($request, [
            'slug' => 'required|string|unique:statuses,slug,' . $status->id,
            'font_color' => 'required|string',
            'bg_color' => 'required|string',
        ]);

        $status->update([
            'slug' => $request->slug,
            'font_color' => $request->font_color,
            'bg_color' => $request->bg_color,
        ]);

        $this->info(trans('admin.messages.updated'));
        return redirect()->route('dashboard.statuses.index');
    }

    public function destroy($statusId)
    {
        $status = Status::findOrFail($statusId);

        $status->delete();

        $this->info(trans('admin.messages.deleted'));
        return redirect()->back();
    }
}
