<?php

namespace App\Http\Controllers\Web;

use Inertia\Inertia;
use App\Models\Export;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\ExportResource;

class LineController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $lines = Export::whereLocale(app()->getLocale())->lines()->get();

        return Inertia::render('Lines', [
            'lines' => ExportResource::collection($lines),
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'ids' => ['required', 'array', 'min:1'],
            'ids.*' => ['exists:exports,id'],
        ]);

        Export::whereIn('id', $request->ids)->update([
            'is_line' => true
        ]);

        return redirect()->route('lines');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Export  $export
     * @return \Illuminate\Http\Response
     */
    public function destroy(Export $export)
    {
        $export->update(['is_line' => false]);

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