<?php

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

use App\Customer;
use App\Http\Controllers\Controller;
use App\RentalProperty;
use App\RentalPropertyEnquiry;
use Carbon\Carbon;

class RentalPropertyEnquiryController extends Controller
{
    public function index()
    {
        $enquiries = [];
        $rentalPropertyEnquiries = RentalPropertyEnquiry::select('id', 'rental_property_id', 'customer_id', 'created_at')
            ->groupBy('rental_property_id', 'customer_id')
            ->get();
        foreach ($rentalPropertyEnquiries as $key => $rentalPropertyEnquiry) {
            $rentalProperty = RentalProperty::find($rentalPropertyEnquiry->rental_property_id);
            $saleActivity = $rentalProperty ? $rentalProperty->saleActivity : null;
            $priceList = $saleActivity ? $saleActivity->getProjectUnit : null;
            $salesOwner = $saleActivity ? $saleActivity->getSalesOwner : null;
            $proposal = $saleActivity ? $saleActivity->getFinalProposal : null;
            $project = $saleActivity ? $saleActivity->getProject : null;
            $customer = Customer::find($rentalPropertyEnquiry->customer_id);
            $enquiries[] = [
                'id' => $rentalPropertyEnquiry->id,
                'project_name' => $project ? $project->project_name : null,
                'customer_name' => $customer ? $customer->getFullNameAttribute() : null,
                'phone_no' => $customer ? $customer->mobile: null,
                'status' => $rentalProperty ? $rentalProperty->status : null,
                'rent_amount' => $rentalProperty ? $rentalProperty->rent_amount : null,
                'submit_date' => $rentalProperty ? Carbon::parse($rentalProperty->created_at)->format('Y-m-d H:i:s') : null,
                'property_owner' => $salesOwner ? $salesOwner->name : null,
                'car_parking_no' => $saleActivity ? $saleActivity->car_parking : null,
                'furnish_status' => $rentalProperty ? $rentalProperty->furnish_status : null,
                'super_build_up_area' => $proposal ? $proposal->total_sba : null,
                'floor' => $priceList ? $priceList->floor : null,
                'type' => $priceList ? $priceList->apartment_type : null,
            ];
        }

        return $this->success(
            'Rental Property Enquiries!',
            $enquiries
        );
    }
}
