<?php

namespace App\Http\Controllers;

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

class DashboardController extends Controller
{
    public function clientDashboard()
    {

        $projects = Project::where("user_id", Auth::user()->id)->orderBy('created_at','desc')
            ->with("files", "location", "skills", "LevelOfExperience")->paginate(5);




        $client = Auth::user();

        $services = $client->services();

        foreach ($client->serviceOrders as $order){
            if ($order->milestones->first->transaction ==null){
                $order->delete();
            }
        }

        $client->load('location', 'wallet', 'serviceOrders');

        $client->loadCount('projects', 'serviceOrders', 'chats');


        $client->append('proposal_count','active_orders_count','processing_projects_count');
        return Inertia::render("Dashboard/Client/Summary/Index", [
            "projects" => $projects,
            "services" => $services,
            "client" => $client,
        ]);

    }

    public function engineerDashboard(Request $request)
        {
            try {


        $engineer = Auth::user()->engineer;

        $projects = $engineer->projects()->where('status','Processing')->with('files', 'location', 'skills', 'LevelOfExperience')->paginate(3);

        $services = $engineer->orderedServices();
        $engineer->load('location', 'wallet', 'user');
        $engineer->append('successful_jobs','processing_projects_count','active_orders_count');
        $engineer->loadCount('projects', 'chats', 'proposals',);

        return Inertia::render("Dashboard/Engineer/Summary/Index", [
            "services" => $services,
            "projects" => $projects,
            "engineer" => $engineer,
        ]);
            }catch (\Exception $e){

            }
    }
    public function firmDashboard(Request $request)
        {
            try {



        $firm = Auth::user()->firm;

        $projects = $firm->projects()->where('status','Processing')->with('files', 'location', 'skills', 'LevelOfExperience')->paginate(3);

        // dd($firm->projects);
        $services = $firm->orderedServices();
        $firm->load('location', 'wallet', 'user');
        $firm->append('successful_jobs','processing_projects_count','active_orders_count');
        $firm->loadCount('projects', 'chats', 'proposals',);

        return Inertia::render("Dashboard/Firm/Summary/Index", [
            "services" => $services,
            "projects" => $projects,
            "firm" => $firm,
        ]);
            }catch (\Exception $e){

            }
    }
}
