<?php

namespace App\Api\Http\Controllers\Api\V2\CustomerScheduleEvent;

use App\Api\Http\Requests\CustomerEvent\CustomerEventRequest;
use App\Customer;
use App\CustomerScheduleEvent;
use App\Http\Controllers\Controller;
use App\PreferredProject;
use App\User;
use Carbon\Carbon;

class CustomerScheduleEventController extends Controller
{
    public function getSiteVisitors()
    {
        $data = [];
        $customerScheduleEvents = CustomerScheduleEvent::where('type', 'sitevisit')->orderBy('id', 'desc')->get();
        foreach ($customerScheduleEvents as $key => $customerScheduleEvent) {
            $project = PreferredProject::find($customerScheduleEvent->project_id);
            $customer = Customer::find($customerScheduleEvent->customer_id);
            $data[] = [
                'id' => $customerScheduleEvent->id,
                'project_name' => $project ? $project->project_name : null,
                'customer_name' => $customer ? $customer->getFullNameAttribute() : null,
                'phone_no' => $customer ? $customer->mobile : null,
                'date' => $customerScheduleEvent->date,
                'time' => $customerScheduleEvent->time,
                'status' => $customerScheduleEvent->status,
            ];
        }
        return $this->success(
            'Site Visitors!',
            $data
        );
    }

    public function getAppoitmentCustomers()
    {
        $data = [];
        $customerScheduleEvents = CustomerScheduleEvent::where('type', 'appointment')->orderBy('id', 'desc')->get();
        foreach ($customerScheduleEvents as $key => $customerScheduleEvent) {
            $customer = Customer::find($customerScheduleEvent->customer_id);
            $data[] = [
                'id' => $customerScheduleEvent->id,
                'customer_name' => $customer ? $customer->getFullNameAttribute() : null,
                'phone_no' => $customer ? $customer->mobile : null,
                'date' => $customerScheduleEvent->date,
                'time' => $customerScheduleEvent->time,
                'status' => $customerScheduleEvent->status,
            ];
        }
        return $this->success(
            'Appointments!',
            $data
        );
    }

    public function getCallbackCustomers()
    {
        $data = [];
        $customerScheduleEvents = CustomerScheduleEvent::where('type', 'callback')->orderBy('id', 'desc')->get();
        foreach ($customerScheduleEvents as $key => $customerScheduleEvent) {
            $customer = Customer::find($customerScheduleEvent->customer_id);
            $data[] = [
                'id' => $customerScheduleEvent->id,
                'customer_name' => $customer ? $customer->getFullNameAttribute() : null,
                'phone_no' => $customer ? $customer->mobile : null,
                'date' => $customerScheduleEvent->date,
                'time' => $customerScheduleEvent->time,
                'status' => $customerScheduleEvent->status,
            ];
        }
        return $this->success(
            'Callback!',
            $data
        );
    }

    public function customerEventRequest(CustomerEventRequest $request)
    {
        try {
            $customerScheduleEventId = $request->request_id;
            $salePersonId = $request->allocate_to;
            $date = $request->date ? Carbon::parse($request->date)->format('Y-m-d') : null;
            $time = $request->time ? Carbon::parse($request->time)->format('H:i:s') : null;
            $status = "Confirmed";

            $customerScheduleEvent = CustomerScheduleEvent::find($customerScheduleEventId);
            $customerScheduleEvent->sale_person_id = $salePersonId;
            $customerScheduleEvent->date = $date;
            $customerScheduleEvent->time = $time;
            $customerScheduleEvent->status = $status;
            if ($customerScheduleEvent->update()) {
                return $this->success(
                    'Customer Schedule Event Updated Successfully',
                );
            } else {
                return $this->error(
                    'Customer Schedule Event Failed To Update!',
                );
            }
        } catch (\Throwable $th) {
            return $this->error(
                'Something went wrong!'
            );
        }
    }

    public function getActiveUsers()
    {
        $users = User::where('status', 1)->get();

        return $this->success(
            'Active Users!',
            $users
        );
    }
}
