<?php

namespace App\Http\Controllers;

use App\Models\Location;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;

class LocationController extends Controller
{
    //
    public function store()
    {
        DB::transaction(function () {
            request()->validate([
                'engineer_id' => Rule::exists('engineers', 'id'),
                'long' => 'double|required',
                'lat' => 'double|required',
                'address' => 'required',
                'user_id' => 'int',
                'pending_firm_id' => 'uuid',
            ]);
            Location::create(request()->attributes);
        });
    }

    public function update(Location $location)
    {

        request()->validate([
            'user_id' => 'int',
            'long' => 'double|required',
            'lat' => 'double|required',
            'address' => 'required',
        ]);
        $location->update(request()->attributes);

        return back(303)->with(['success' => 'success']);

    }

    public function destroy(Location $location)
    {
        $location->delete();
        return back(303)->with(['success' => 'success']);
    }
}
