<?php

namespace App\Console\Commands\Jobs;

use App\Models\Property;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Artisan;

class DataFetch extends Command
{


    protected $signature = 'cron:data-fetch';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {

        $propertyList = Property::whereIn('status', [1, 3])
            ->where('commission', '>', 1)
            //->where('id', 1456)
            ->with('propertyContractUser')
            ->with('propertyExecutive.executiveType')
            ->with('propertyContact')
            ->with('propertyAdditionalInfos.propertyAdditionalInfoKey')
            ->with('propertyType')
            ->with('propertyStatus')
            ->get();

        $propertyList = $propertyList ? $propertyList->toArray() : null;


        $propertyListToExcel = [];
        foreach ($propertyList as $property) {

            $numberOfRooms = collect($property['property_additional_infos'])->where('additional_info_key_id',3)->first();
            $numberOfRooms = $numberOfRooms['value'];

            $propertyListToExcel[] = [
                'contract_user' => $property['property_contract_user']['nameSurname'],
                'executive_name_surname' => reset($property['property_executive'])['name_surname'],
                'name' => $property['name'],
                'phone' => $property['property_contact']['view_full_phone'],
                'email' => $property['property_contact']['email'],
                'executive_position' => reset($property['property_executive'])['executive_type']['name'],
                'executive_phone' => reset($property['property_executive'])['view_full_phone'],
                'executive_email' => reset($property['property_executive'])['email'],
                'web' => $property['property_contact']['web'],
                'address' => $property['property_contact']['address'],
                'location' => null, //Bölge
                'number_of_rooms' => $numberOfRooms,
                'category' => $property['property_type']['name'],
                'property_pms' => null,
                'property_cm' => null,
                'commission' => $property['commission'],
                'commission_period' => $property['invoice_type'],
                'contract_start' => Carbon::createFromTimestamp($property['created_at'])->format('d.m.Y'),
                'contract_finish' => null,
                'content' => null,
                'dns' => null,
                'ssl' => null,
                'cm' => null,
                'third_party' => null,
                'golive' => Carbon::createFromTimestamp($property['created_at'])->format('d.m.Y'),
                'training' => null,
                'panel' => null,
                'official_name' => $property['official_name'],
                'tax_office' => $property['tax_office'],
                'tax_number' => $property['tax_number'],
                'tax_address' => $property['property_contact']['address'],
                'status' => $property['property_status']['name'],
                'id' => $property['id'],
            ];

        }

        $f = fopen("C:\www\api.extranetwork.com\storage/tmp.csv", "w");
        foreach ($propertyListToExcel as $property) {
            fputcsv($f, $property);
        }


    }

}
