<?php

namespace App\Http\Controllers;

use App\Actions\Fortify\CreateNewUser;
use App\Models\Engineer;
use App\Models\Firm;
use App\Models\FirmSize;
use App\Models\FirmType;
use App\Models\LevelOfExperience;
use App\Models\Skill;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Redirect;
use Laravel\Jetstream\Jetstream;
use Rinvex\Categories\Models\Category;

class RegistrationController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        $createNewUser = new CreateNewUser();
        $user = $createNewUser->create($request->all());
        Auth::login($user);
        return Redirect::route('client.dashboard');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create(Request $request)
    {

        $categories = Category::where('parent_id', null)->where('verified', 1)->with(['descendants' => function($q) {
                $q->where('verified', 1); 
            }])->get();
        $skills = Skill::all();

        if ($request->type == 'client') return Jetstream::inertia()->render($request, 'Auth/Register/Client', ['type' => 'client']);

        elseif ($request->type == 'engineer') {
            $levelsOfExperience = LevelOfExperience::all();
            return Jetstream::inertia()->render($request, 'Auth/Register/Engineer', ['type' => 'engineer', 'levelsOfExperience' => $levelsOfExperience, 'categories' => $categories, 'skills' => $skills]);
        } elseif ($request->type == 'firm') {
            $firmSize = FirmSize::all();
            // $firmTypes = FirmType::where('verified', true)->get();
            $firmTypes = FirmType::all();
            return Jetstream::inertia()->render($request, 'Auth/Register/Firm', ['type' => 'firm', 'firmSize' => $firmSize, 'categories' => $categories, 'firmTypes' => $firmTypes, 'skills' => $skills]);
        }

        return Jetstream::inertia()->render($request, 'Auth/Register/index');
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

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

    /**
     * Complete the specified resource.
     *
     * @param Request $request
     * @param int $id
     * @return Response
     */
    public function complete(Request $request)
    {
        $entity = null;
        $email = Crypt::decryptString($request->id);
        // dd($request->type);
        if($request->type == 'firm') 
            $entity = Firm::where('email', $email)->first();
        if($request->type == 'engineer') 
            $entity = Engineer::where('email', $email)->first();

        if(!$entity) return Redirect::route('register')->with('error', 'Invalid information provided, please try again');
        if($entity?->user?->email_verified_at) return Redirect::route('login')->with('success', 'Account already verified, please login');
        
        if($request->type == 'firm') return Jetstream::inertia()->render($request, 'Auth/Confirm/Firm', ['firm' => $entity]);
        if($request->type == 'engineer') return Jetstream::inertia()->render($request, 'Auth/Confirm/Engineer', ['engineer' => $entity]);

    }

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