<?php

namespace App\Http\Controllers;

use App\Models\Service;
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 FirmServicesController extends Controller
{
    public function __construct()
    {
        $this->middleware('service');
    }
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $firm = Auth::user()
            ->firm()
            ->with(
                "services.images",
                "services.skills",
                "services.location",
                "services.tierOptions",
                "services.orders",
                "user",
                "currency",
            )
            ->firstOrFail();
        $drafts = Service::getAllDrafts()->where('serviceable_id','=',$firm->id)->where('serviceable_type','=','App\Models\Firm');
//        foreach ()
        foreach ($drafts as $draft){

            $draft->slug ??= Str::uuid()->toString();
            $draft->images =[];
            $draft->location=null;
            $draft->tierOptions =[];
            $draft->orders =[];
            $draft->skills=[];
        }
        $firm->services = collect([$drafts,$firm->services])->flatten();
//        dd($drafts,$firm->services,$firm);
        $orders = $firm->servicesOrders;
//        dd($orders);
        return Inertia::render("Services/Firm/Index", [
            "firm" => $firm,
            "services" => $firm->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();
        $firm = Auth::user()
            ->firm()
            ->with("currency")
            ->firstOrFail();
        return Inertia::render("Services/Firm/Create/Show", [
            "categories" => $categories,
            "firm" => $firm,
        ]);
    }

    /**
     * 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/Firm/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();
        $firm = Auth::user()
            ->firm()
            ->with("currency")
            ->firstOrFail();
        return Inertia::render("Services/Firm/Update/Show", [
            "categories" => $categories,
            "firm" => $firm,
            "service" => $service,
        ]);
    }

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