<?php

namespace App\Http\Controllers;

use App\Models\Service;
use App\Models\ServiceOrder;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Inertia\Inertia;
use Rinvex\Categories\Models\Category;

class EngineerServicesController extends Controller
{
    public function __construct()
    {
        $this->middleware('service');
    }
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $engineer = Auth::user()
            ->engineer()
            ->with(
                "services.images",
                "services.skills",
                "services.location",
                "services.tierOptions",
                "services.orders",
                "user",
                "currency"
            )
            ->firstOrFail();

        $drafts = Service::getAllDrafts()->where('serviceable_id','=',$engineer->id)->where('serviceable_type','=','App\Models\Engineer');
//        foreach ()
        foreach ($drafts as $draft){

            $draft->slug ??= Str::uuid()->toString();
            $draft->images =[];
            $draft->location=[];
            $draft->tierOptions =[];
            $draft->orders =[];
            $draft->skills=[];
        }
        $engineer->services = collect([$drafts,$engineer->services])->flatten();
//        dd($drafts,$engineer->services,$engineer);
        $orders = $engineer->servicesOrders;

        return Inertia::render("Services/Engineer/Index", [
            "engineer" => $engineer,
            "services" => $engineer->services,
            "orders" => $orders
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        $categories = Category::where("parent_id", null)
            ->with("descendants")
            ->get();
        $engineer = Auth::user()
            ->engineer()
            ->with("currency")
            ->firstOrFail();
        return Inertia::render("Services/Engineer/Create/Show", [
            "categories" => $categories,
            "engineer" => $engineer,
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     *
     * @return Response
     */
    public function show($slug)
    {
        $service = Service::where('slug',$slug)->first() ?? Service::getAllDrafts()->where('slug',$slug)->firstOrFail();
        $service->load(
            "categories",
            "skills",
            "location",
            "tierOptions",
            "images"
        );
        $service->slug ??= Str::uuid()->toString();
        $service->images ??=[];
        $service->tierOptions ??=[];
        $service->orders ??=[];
        $service->skills ??=[];

        return Inertia::render("Services/Engineer/Single/Index", [
            "service" => $service,
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param int $id
     * @return Response
     */
    public function update($slug)
    {
        $service = Service::where('slug',$slug)->first() ?? Service::getAllDrafts()->where('slug',$slug)->firstOrFail();
        $service->load(
            "categories",
            "skills",
            "location",
            "tierOptions",
            "images"
        );
        $service->slug ??= Str::uuid()->toString();
        $service->images ??=[];
        $service->tierOptions ??=[];
        $service->orders ??=[];
        $service->skills ??=[];
        $categories = Category::where("parent_id", null)
            ->with("descendants")
            ->get();
        $engineer = Auth::user()
            ->engineer()
            ->with("currency")
            ->firstOrFail();
        return Inertia::render("Services/Engineer/Update/Show", [
            "categories" => $categories,
            "engineer" => $engineer,
            "service" => $service,
        ]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
}
