<?php

namespace App\Http\Controllers;

use App\Models\Contract;
use App\Models\Language;
use App\Models\LevelOfExperience;
use App\Models\Milestone;
use App\Models\Project;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;

class WorkerProjectsController extends Controller
{
    public function __construct()
    {
//        $this->middleware('project');
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function showEngineer($slug)
    {
        $project= Project::where('slug',$slug)->firstOrFail();
        $engineer = Auth::user()->engineer;
        if($engineer==null && Auth::user()->hasAnyPermission(['control projects'])) return redirect()->route('project.show',$project->slug);
        else if ($engineer==null) return redirect()->route('projects.index')->with('error', 'You are not allowed to access this project.');
        $languages = Language::all();
        $levelsOfExperience = LevelOfExperience::all();

        $project->load([
            "categories",
            "skills",
            "location",
            "files",
            'languageMasteries',
            'contract',
            'user.location',
            ]
        );

        // dd($engineer);

        $hasAcceptedProposal =  $project->workerAcceptedProposal($engineer);

        $hasUnacceptedProposal = $hasAcceptedProposal ? false : $project->proposals()->with(["proposalable.user", "files", 'contract', 'milestones' => ['files', 'deliverables']])->where('proposalable_id', $engineer->id)->where('proposalable_type', 'App\Models\Engineer')->first();

        $project['language'] = $project->languageMasteries[0]['language'];


        return Inertia::render("Projects/Engineer/Single/Index", [
            "project" => $project,
            "languages" => $languages,
            "levelsOfExperience" => $levelsOfExperience,
            "hasAcceptedProposal" => $hasAcceptedProposal,
            "hasUnacceptedProposal" => $hasUnacceptedProposal,
        ]);
    }

    public function showFirm($slug)
    {
        $project= Project::where('slug',$slug)->firstOrFail();
        $firm = Auth::user()->firm;
        if($firm==null && Auth::user()->hasAnyPermission(['control projects'])) return redirect()->route('project.show',$project->slug);
        else if ($firm==null) return redirect()->route('projects.index')->with('error', 'You are not allowed to access this project.');
        $languages = Language::all();
        $levelsOfExperience = LevelOfExperience::all();

        $project->load([
            "categories",
            "skills",
            "location",
            "files",
            'languageMasteries',
            'contract',
            'user.location',
            ]
        );


        $hasAcceptedProposal =  $project->workerAcceptedProposal($firm);

        $hasUnacceptedProposal = $hasAcceptedProposal ? false : $project->proposals()->with(["proposalable.user", "files", 'contract', 'milestones' => ['files', 'deliverables']])->where('proposalable_id', $firm->id)->where('proposalable_type', 'App\Models\Firm')->first();

        $project['language'] = $project->languageMasteries[0]['language'];


        return Inertia::render("Projects/Engineer/Single/Index", [
            "project" => $project,
            "languages" => $languages,
            "levelsOfExperience" => $levelsOfExperience,
            "hasAcceptedProposal" => $hasAcceptedProposal,
            "hasUnacceptedProposal" => $hasUnacceptedProposal,
        ]);
    }



    public function WorkerSigned(Contract $contract){
        $contract->contractorSign();
        return back(303)->with(['success' => 'success']);
    }

    public function AddMilestoneDeliverable(Request $request){

        $milestone = Milestone::findOrFail($request->milestone_id);

        $milestone->addFiles($request->attachements);

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

    }
}
