<?php

namespace App\Services\Admin;

use App\Models\Attribute;
use App\Models\Offplan;
use App\Models\Listing;
use App\Services\BaseService;
use Illuminate\Support\Facades\Auth;
use Yajra\Datatables\Datatables;

class OffplanService extends BaseService
{
    public function offplansList($request)
    {
        $offplansList = Offplan::with('project', 'sale_status', 'development_status', 'unit_type', 'developer');
        if ($request->has('project_search') && $request['project_search'] != '' && $request['project_search'] != 'null' && $request['project_search'] != 'undefined') {
            $offplansList->where('project_id', $request['project_search']);
        }
        if ($request->has('unit_type_search') && $request['unit_type_search'] != '' && $request['unit_type_search'] != 'null' && $request['unit_type_search'] != 'undefined') {
            $offplansList->where('unit_type_id', $request['unit_type_search']);
        }
        if ($request->has('sale_status_search') && $request['sale_status_search'] != '' && $request['sale_status_search'] != 'null' && $request['sale_status_search'] != 'undefined') {
            $offplansList->where('sale_status_id', $request['sale_status_search']);
        }
        if ($request->has('development_status_search') && $request['development_status_search'] != '' && $request['development_status_search'] != 'null' && $request['development_status_search'] != 'undefined') {
            $offplansList->where('development_status_id', $request['development_status_search']);
        }
        if ($request->has('developer_search') && $request['developer_search'] != '' && $request['developer_search'] != 'null' && $request['developer_search'] != 'undefined') {
            $offplansList->where('developer_id', $request['developer_search']);
        }
        $datatable = Datatables::of($offplansList);
        return collect($datatable->make(true)->getData());
    }

    public function getOffplanDetail($id)
    {
        $offplan = Offplan::with('project')->findOrFail($id);
        if ($offplan->added_by == Auth::id()) {
            return $offplan;
        }
    }

    public function createOffplan(array $data)
    {
        $attr = new Offplan();
        $attr->project_id = $data['project_id'] ?? null;
        $attr->unit_type_id = $data['unit_type_id'] ?? null;
        $attr->developer_id = $data['developer_id'] ?? null;
        $attr->development_status_id = $data['development_status_id'] ?? null;
        $attr->sale_status_id = $data['sale_status_id'] ?? null;
        $attr->unit_no = $data['unit_no'] ?? null;
        $attr->floor = $data['floor'] ?? null;
        $attr->view = $data['view'] ?? null;
        $attr->price = $data['price'] ?? null;
        $attr->area = $data['area'] ?? null;
        $attr->unit_area = $data['unit_area'] ?? null;
        $attr->beds = $data['beds'] ?? null;
        $attr->completion_date = $data['cd'] ?? null;
        $attr->parking = $data['parking'] ?? null;
        $attr->added_by = Auth::id();
        $attr->save();
    }

    public function updateOffplan($data, $id)
    {
        $attr = Offplan::findOrFail($id);
        if ($attr->added_by == Auth::id()) {
            $attr->district_id = $data['district_id'] ?? null;
            if ($data['project_id']) {
                $pro = Attribute::find($data['project_id']);
                if ($pro && $pro->parent) {
                    $attr->district_id = $pro->parent->id;
                }
                $attr->project_id = $data['project_id'] ?? null;
            }
            $attr->contact_id = $data['contact_id'] ?? null;
            $attr->purpose_id = $data['purpose_id'] ?? null;
            $attr->property_type_id = $data['property_type_id'] ?? null;
            $attr->offplan_status_id = $data['offplan_status_id'] ?? null;
            $attr->offplan_category_id = $data['offplan_category_id'] ?? null;
            $attr->offplan_type_id = $data['offplan_type_id'] ?? null;
            $attr->offplan_source_id = $data['offplan_source_id'] ?? null;
            $attr->offplan_interest_level_id = $data['offplan_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->unit_no = $data['unit_no'] ?? null;
            $attr->price = $data['price'] ?? null;
            $attr->area = $data['area'] ?? null;
            $attr->beds = $data['beds'] ?? null;
            $attr->baths = $data['baths'] ?? null;
            $attr->updated_by = Auth::id();
            $attr->save();
        }
    }
}
