<?php

namespace App\Http\Controllers;

use App\Models\Domain;
use App\Services\XeroService;
use Illuminate\Http\Request;
use App\Models\XeroCustomer;

class XeroCustomersController extends Controller
{
    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function index()
    {
        $xeroCustomers = XeroCustomer::paginate(15);

        return view('xero_customers.index', compact('xeroCustomers'));
    }

    /**
     * @param $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     * @throws \Exception
     */
    public function show($id)
    {
        $xeroCustomer = XeroCustomer::findOrFail($id);
        $xero = new XeroService();
        $xeroTaxes = $xero->getXeroTaxes();

        return view('xero_customers.show', compact('xeroCustomer', 'xeroTaxes'));
    }

    /**
     * @param $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     * @throws \Exception
     */
    public function edit($id)
    {
        $xeroCustomer = XeroCustomer::findOrFail($id);
        $xero = new XeroService();
        $xeroTaxes = $xero->getXeroTaxes();

        return view('xero_customers.edit', compact('xeroCustomer', 'xeroTaxes'));
    }

    /**
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'tax_type' => 'required|string',
        ]);
        $validatedData = $request->all();
        $xeroCustomer = XeroCustomer::findOrFail($id);
        $xeroCustomer->tax_type = $validatedData['tax_type'];
        $xeroCustomer->save();

        return redirect()->route('xero-customers.show', $id);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     * @throws \Illuminate\Validation\ValidationException
     */
    public function search(Request $request)
    {
        $this->validate($request, [
            'search' => 'string',
        ]);
        $validatedData = $request->all();
        $xeroCustomers = XeroCustomer::where('xero_name', 'like', '%' . $validatedData['search'] . '%')
            ->orWhere('xero_account_number', 'like', '%' . $validatedData['search'] . '%')
            ->get();
        $search_query = $validatedData['search'];

        return view('xero_customers.search', compact('xeroCustomers', 'search_query'));
    }

    /**
     *  xeroCustomersWithoutEmail
     */
    public function xeroCustomersWithoutEmail()
    {
        $domainsArray = [];
        $domains = Domain::where('deleted_at', null)->get();
        foreach ($domains as $key => $domain) {
            $domainsArray[] = $domain->customer_id;
        }
        $domainsArray = array_unique($domainsArray);
        foreach ($domainsArray as $key => $customer_id) {
            $customer = XeroCustomer::find($customer_id);
            if( $customer ) {
                if( $customer->email == null ) {
                    echo 'Xero customer '.$customer ->xero_name.' has no email address record in xero'.'<br>';
                }
            } else {
                echo $domain->domain.'<br>';
            }
        }
    }

}
