<?php

namespace App\Http\Controllers;

use App\Http\Controllers;
use App\Models\Client;
use App\Models\Project;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class ProjectController extends Controller
{
    //Companies
    public function companies()
    {
        $projects = Project::paginate(20);
        return view('admin.projects.list',[
            'projects'=>$projects
        ]);
    }
    //Create
    public function create()
    {
        $customers = Client::orderBy('nms', 'asc')->get();
        return view('admin.projects.create', compact('customers'));
    }
    //SUBMIT
    public function submit(Request $request)
    {
        $validatedData = $request->validate([
            'customerId' => 'required',
            'projectName' => 'string',
            'projectHandler' => 'string',
            'expectedStartDate' => 'required',
            'expectedDuration' => 'numeric',
            'projectLocation' => 'required',
            'projectDetails' => 'string',
        ]);

        $expectedEndDate = Carbon::parse($request['expectedStartDate'])->addDays($request['expectedDuration']);
        $createProject = Project::create([
            'customerId'=>$request->input('customerId'),
            'projectName'=>$request->input('projectName'),
            'projectHandler'=>$request->input('projectHandler'),
            'expectedStartDate'=>$request->input('expectedStartDate'),
            'expectedDuration'=>$request->input('expectedDuration'),
            'expectedEndDate'=>$expectedEndDate,
            'projectLocation'=>$request->input('projectLocation'),
            'projectDetails'=>$request->input('projectDetails'),
        ]);
        if (!$createProject)
        {
            return back()->withInput()->with('error','An unexpected error occurred.Please try again.');
        }
        return back()->with('success','Project Created successfully.');
    }
    //Edit
    public function edit($id)
    {
        $details = Client::findorfail($id);
        return view('admin.customers.edit',[
            'details'=>$details
        ]);
    }
    //Update
    public function update(Request $request,$id)
    {
        $validatedData = $request->validate([
            'name' => 'required|max:255',
            'location' => 'required',
            'details' => 'string',
            'image'=>'mimes:jpeg,jpg,png,bmb,gif,svg',
        ]);

        $createCompany = Client::where('id',$id)->update([
            'name'=>$request->input('name'),
            'location'=>$request->input('location'),
            'details'=>$request->input('details')
        ]);
        if (!$createCompany)
        {
            return back()->withInput()->with('error','An unexpected error occurred.Please try again.');
        }
        if ($request->hasFile('image'))
        {
            //Upload the damn image
            $filenamewithextension = $request->file('image')->getClientOriginalName();

            //get filename without extension
            $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);

            //get file extension
            $extension = $request->file('image')->getClientOriginalExtension();

            //filename to store
            $filenametostore = 'blog/'.time().'.'.$extension;

            //Upload File to s3
            $storeImage = Storage::disk('public')->put($filenametostore, fopen($request->file('image'), 'r+'), 'public');

            $fileUrl = $filenametostore;
            if ($storeImage)
            {
                $storePath = Client::where('id',$id)
                    ->update([
                        'logo'=>$fileUrl,
                    ]);
                return back()->with('success','Insurance Company Updated successfully.');
            }

            return back()->withInput()->with('errors','An unexpected error occurred.Please reload the page and try again.');
        }
        return back()->with('success','Insurance Company Updated successfully.');
    }
    //Delete companies
    public function delete($id)
    {
        $details = Client::findorfail($id);
        $details->delete();
        return back()->with('success','Company deleted successfully.');
    }
}
