<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 
use App\Models\User;
use App\Models\HouseType;
use App\Models\HeatingFuel;
use App\Models\Amenity;
use App\Models\AgeProperty;
use App\Models\SqftProperty;
use App\Models\Parking;
use App\Models\Province;
use App\Models\Bedroom;
use App\Models\Bathroom;
use App\Models\Cms;
use App\Models\TermCondition;
use App\Models\RealtorYear;
use App\Models\Language;
use App\Models\MessageThread;
use Illuminate\Support\Facades\Auth; 

class MasterController extends Controller {
    
    public function index(Request $request){
        
        $provinces       = Province::with('regions','regions.cities')->where('status',1)->orderBy('ordering','asc')->get();
        $ageProperty     = AgeProperty::select('name')->where('status', 1)->orderBy('ordering', 'asc')->get();
        $typeOfProperty  = HouseType::select('name')->where('status', 1)->orderBy('ordering', 'asc')->get();
        $sqftProperty    = SqftProperty::select('name', 'type_of_property')->where('status', 1)->orderBy('ordering', 'asc')->get();
        $parkings        = Parking::select('name')->where('status', 1)->orderBy('ordering', 'asc')->get();
        $bedrooms        = Bedroom::select('bedroom')->where('status', 1)->orderBy('ordering', 'asc')->get();
        $bathrooms       = Bathroom::select('bathroom')->where('status', 1)->orderBy('ordering', 'asc')->get();
        $heatingFuels    = HeatingFuel::select('name')->where('status', 1)->orderBy('ordering', 'asc')->get();
        $amenities       = Amenity::select('name')->where('status', 1)->orderBy('ordering', 'asc')->get();
        $cms             = Cms::select('title', 'description')->where('status', 1)->orderBy('ordering', 'asc')->get();
        $realtorYears    = RealtorYear::select('name')->where('status', 1)->orderBy('ordering', 'asc')->get();
        $language        = Language::select('name')->where('status', 1)->orderBy('ordering', 'asc')->get();
        
        $propertyTypeArray = [];
        foreach($typeOfProperty as $sqftPropertyKey => $sqftPropertyVal) {
            $propertyTypeValue = $sqftPropertyVal->name;
            
            $sqftArray        = SqftProperty::select('name')->where('status', 1)->where('type_of_property','LIKE','%'.$propertyTypeValue.'%')->get();
            
            $propertyTypeArray[$sqftPropertyKey]['name'] = $propertyTypeValue;
            foreach($sqftArray as $sqftRow) {
                $propertyTypeArray[$sqftPropertyKey]['sqft'][] = $sqftRow->name;
            }
            
        }
    
        $fieldArray = [];
        $data = [
            'provinces'       => $provinces,
            'ageProperty'     => $ageProperty,
            'typeOfProperty'  => $propertyTypeArray,
            'sqftProperty'    => $sqftProperty,
            'parkings'        => $parkings,
            'bedrooms'        => $bedrooms,
            'bathrooms'       => $bathrooms,
            'heatingFuels'    => $heatingFuels,
            'amenities'       => $amenities,
            'cms'             => $cms,
            'realtorYears'    => $realtorYears,
            'language'        => $language,
        ];
        $fieldArray[] = $data;
        
        return response()->json([
            'success' => 1,
            'data' => $fieldArray
        ], 200);
        
    }
    
    public function reportUserOptions(Request $request) {
        $options   = [];
        $options[] = 'Should be tagged as adult content';
        $options[] = 'Offensive (rude, obscene)';
        $options[] = 'Spam (asd, self-promotion)';
        $options[] = 'Off topic (Trolling)';
        
        return response()->json([
            'success' => 1,
            'text'   => 'Are you sure you want to report this user? once you report this user you will not be able to longer accessible of this conversation.',
            'options' => $options
        ], 200);
        
    }
    
    public function saveReportUser(Request $request) {
        $messageThreadId = request('message_thread_id');
        $message         = request('message');
        
        MessageThread::where('id',$messageThreadId)->update([
            'block' => 1,
            'report_message' => $message
        ]);
        
        return response()->json([
            'success' => 1,
            'message'   => 'Sucessfully Reported.'
        ], 200);
    }
}