<?php

namespace App\Services\Admin;

use App\Events\UserLogEvent;
use App\Models\Lead;
use App\Models\Listing;
use App\Services\BaseService;
use Illuminate\Support\Facades\Auth;
use Yajra\Datatables\Datatables;

class LeadService extends BaseService
{
    public function leadsList($request)
    {
        $leadsList = Lead::with('assignee', 'lead_type', 'lead_category', 'lead_status', 'property_type')->where('added_by', Auth::id());
        if ($request->has('lead_status_search') && $request['lead_status_search'] != '' && $request['lead_status_search'] != 'null' && $request['lead_status_search'] != 'undefined') {
            $leadsList->whereIn('lead_status_id', $request['lead_status_search']);
        }
        if ($request->has('lead_category_search') && $request['lead_category_search'] != '' && $request['lead_category_search'] != 'null' && $request['lead_category_search'] != 'undefined') {
            $leadsList->whereIn('lead_category_id', $request['lead_category_search']);
        }
        if ($request->has('lead_type_search') && $request['lead_type_search'] != '' && $request['lead_type_search'] != 'null' && $request['lead_type_search'] != 'undefined') {
            $leadsList->where('lead_type_id', $request['lead_type_search']);
        }
        if ($request->has('lead_ps_search') && $request['lead_ps_search'] != '' && $request['lead_ps_search'] != 'null' && $request['lead_ps_search'] != 'undefined') {
            $leadsList->where('lead_primary_source_id', $request['lead_ps_search']);
        }
        if ($request->has('lead_ss_search') && $request['lead_ss_search'] != '' && $request['lead_ss_search'] != 'null' && $request['lead_ss_search'] != 'undefined') {
            $leadsList->where('lead_secondary_source_id', $request['lead_ss_search']);
        }
        if ($request->has('at_search') && $request['at_search'] != '' && $request['at_search'] != 'null' && $request['at_search'] != 'undefined') {
            $leadsList->whereIn('assign_to', $request['at_search']);
        }
        if ($request->has('assign_search') && $request['assign_search'] != '' && $request['assign_search'] != 'null' && $request['assign_search'] != 'undefined') {
            if ($request['assign_search'] == 1) {
                $leadsList->whereNotNull('assign_to');
            }
            if ($request['assign_search'] == 2) {
                $leadsList->whereNull('assign_to');
            }
        }
//        $leadsList = $leadsList->where(function ($q) use ($request) {
//            $q->where('added_by', Auth::id());
//        });
        $datatable = Datatables::of($leadsList);
        return collect($datatable->make(true)->getData());
    }

    public function leadListingsList($request)
    {
        $lead = Lead::find($request['lead_id']);
        if ($lead) {
            $area = (int)$lead->area;
            $price = (int)$lead->price;
            $listings = Listing::with('added_by', 'project', 'purpose');
            if ($lead->district_id) {
                $listings->where('district_id', $lead->district_id);
            }
            if ($lead->project_id) {
                $listings->where('project_id', $lead->project_id);
            }
//            $listings->where('purpose_id', $lead->purpose_id);
            if ($lead->area) {
                $listings->whereRaw('CAST(area AS DECIMAL(10, 2)) <= ?', [$area]);
            }
            if ($lead->price) {
                $listings->whereRaw('CAST(price AS DECIMAL(10, 2)) <= ?', [$price]);
            }
//            $listings->where('baths', $lead->baths);
//            $listings->where('beds', $lead->beds);
//            $listings->where('property_type_id', $lead->property_type_id);
//            $listings->where('unit_no', $lead->unit_no);
//            $listingList = $listings->where(['added_by' => auth()->id(), 'type' => 2]);
            $listingList = $listings->where('status', 1);
            $datatable = Datatables::of($listingList);
            return collect($datatable->make(true)->getData());
        }
    }

    public function getLeadDetail($id)
    {
        $lead = Lead::with('project', 'lead_status', 'lead_category', 'assignee')->findOrFail($id);
        if ($lead->added_by == Auth::id()) {
            return $lead;
        }
    }

    public function createLead(array $data)
    {
        $attr = new Lead();
        $attr->lead_type_id = $data['lead_type_id'] ?? null;
        $attr->lead_category_id = $data['lead_category_id'] ?? null;
        $attr->lead_primary_source_id = $data['lead_primary_source_id'] ?? null;
        $attr->lead_secondary_source_id = $data['lead_secondary_source_id'] ?? null;
        $attr->property_type_id = $data['property_type_id'] ?? null;
        $attr->lead_status_id = $data['lead_status_id'] ?? null;
        $attr->lead_interest_level_id = $data['lead_interest_level_id'] ?? null;
        $attr->assign_to = $data['assign_to'] ?? null;
        $attr->title = $data['title'] ?? null;
        $attr->phone = $data['phone'] ?? null;
        $attr->name = $data['name'] ?? null;
        $attr->email = $data['email'] ?? null;
        $attr->description = $data['description'] ?? null;
        $attr->added_by = Auth::id();
        $attr->reference_number = generateReferenceNumber(Auth::id(), 'leads');
        $attr->save();
    }

    public function updateLead($data, $id)
    {
        $attr = Lead::findOrFail($id);
        if ($attr->added_by == Auth::id()) {
            $attr->lead_type_id = $data['lead_type_id'] ?? null;
            $attr->lead_category_id = $data['lead_category_id'] ?? null;
            $attr->lead_primary_source_id = $data['lead_primary_source_id'] ?? null;
            $attr->lead_secondary_source_id = $data['lead_secondary_source_id'] ?? null;
            $attr->property_type_id = $data['property_type_id'] ?? null;
            $attr->lead_status_id = $data['lead_status_id'] ?? null;
            $attr->lead_interest_level_id = $data['lead_interest_level_id'] ?? null;
            $attr->assign_to = $data['assign_to'] ?? null;
            $attr->title = $data['title'] ?? null;
            $attr->phone = $data['phone'] ?? null;
            $attr->name = $data['name'] ?? null;
            $attr->email = $data['email'] ?? null;
            $attr->description = $data['description'] ?? null;
            $attr->added_by = Auth::id();
            $attr->save();
            event(new UserLogEvent($attr->id, 'leads', 'Lead Updated'));
        }
    }
}
