<?php

namespace App\Http\Controllers\Web;

use Inertia\Inertia;
use App\Models\Group;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use Vinkla\Hashids\Facades\Hashids;

class GroupController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $type = request('type', 'import');
        $locale = app()->getLocale();

        return Inertia::render('Groups', [
            'type' => $type,
            'groups' => Group::whereType($type)
                ->when($type === 'import', fn ($q) => $q->withCount(['imports' => fn ($q) => $q->whereLocale($locale)]))
                ->when($type === 'export', fn ($q) => $q->withCount(['exports' => fn ($q) => $q->whereLocale($locale)]))
                ->get(),
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'title' => ['required', 'array'],
            'title.tm' => ['required', 'min:3'],
            'title.ru' => ['required', 'min:3'],
            'title.en' => ['required', 'min:3'],
            'type' => ['string', 'in:import,export'],
            'is_default' => ['boolean'],
        ]);

        if ($request->is_default) {
            Group::whereType(request('type', 'import'))->update(['is_default' => false]);
        }

        Group::create([
            'title' => $request->title,
            'type' => request('type', 'import'),
            'is_default' => $request->is_default
        ]);

        return redirect()->back();
    }

    /**
     * Update
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Group $group)
    {
        $validated = $request->validate([
            'title' => ['required', 'array'],
            'title.tm' => ['required', 'min:3'],
            'title.ru' => ['required', 'min:3'],
            'title.en' => ['required', 'min:3'],
            'is_default' => ['boolean'],
            'type' => ['string', 'in:import,export'],
        ]);

        info($request->all());

        if ($request->is_default) {
            Group::whereType(request('type', 'import'))->update(['is_default' => false]);
        }

        $group->update($validated);

        return redirect()->back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Export  $export
     * @return \Illuminate\Http\Response
     */
    public function destroy(Group $group)
    {
        $group->delete();

        return redirect()->back()->with('success', 'Group deleted');
    }

    /**
     * Download
     *
     * @param  \App\Models\Export  $export
     * @return \Illuminate\Http\Response
     */
    public function download($group)
    {
        $group = Group::where('id', Hashids::decode($group)[0])->firstOrFail();

        abort_if(blank($group->file), 404, 'File not found');

        return Storage::download("uploads/{$group->file}");
    }
}
