<?php

namespace App\Http\Controllers;

use App\Models\map\TjBox;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;

class TjBoxMapController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = [
            'tjBoxes' => TjBox::orderBy('id', 'desc')->paginate(20),
        ];

        return view('tjBoxMap.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('tjBoxMap.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|string|max:255|unique:tj_box_map,name',
            // Must be valid JSON because the DB column is JSON with constraint
            'coordinates' => 'required|json',
            'remarks' => 'nullable|string',
        ]);

        $tjBox = new TjBox();
        $tjBox->name = $request->name;
        // coordinates column is JSON, but map API expects a JSON string,
        // so we store the value exactly as provided from the form.
        $tjBox->coordinates = $request->coordinates;
        $tjBox->type = 'tjBox';
        $tjBox->remarks = $request->remarks;
        $tjBox->save();

        Toastr::success('TJ Box created successfully', 'Success');

        return redirect()->route('tj-box-map.index');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $tjBox = TjBox::findOrFail($id);

        return view('tjBoxMap.edit', [
            'tjBox' => $tjBox,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required|string|max:255|unique:tj_box_map,name,' . $id,
            // Must be valid JSON because the DB column is JSON with constraint
            'coordinates' => 'required|json',
            'remarks' => 'nullable|string',
        ]);

        $tjBox = TjBox::findOrFail($id);
        $tjBox->name = $request->name;
        $tjBox->coordinates = $request->coordinates;
        $tjBox->type = 'tjBox';
        $tjBox->remarks = $request->remarks;
        $tjBox->save();

        Toastr::success('TJ Box updated successfully', 'Success');

        return redirect()->route('tj-box-map.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        // $tjBox = TjBox::findOrFail($id);
        // $loadedTjBox = $tjBox->load('upStream', 'downStream');

        // if ($loadedTjBox->upStream->count() > 0 || $loadedTjBox->downStream->count() > 0) {
        //     Toastr::error('Cannot delete this TJ Box because it has connected components', 'Error');
        //     return redirect()->route('tj-box-map.index');
        // }

        // $loadedTjBox->delete();

        // Toastr::success('TJ Box deleted successfully', 'Success');

        // return redirect()->route('tj-box-map.index');
    }
}

