<?php

namespace Modules\ContactList\Repositories;

use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\ContactList\Interfaces\ContactListRepositoryInterface;
use Modules\ContactList\Models\ContactList;
use Modules\ContactList\Models\ContactListDescription;
use Modules\ContactList\Models\ContactListPhone;

class ContactListRepository implements ContactListRepositoryInterface
{
    public function get(array $request)
    {
        $builder = ContactList::query()
            ->join('contact_list_descriptions AS sd', 'contact_lists.id', 'sd.contact_list_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title','sd.address', 'contact_lists.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        $contactList = ContactList::find($id);

        $query = ContactListDescription::where('contact_list_id', $contactList->id)
            ->join('contact_lists', 'contact_list_descriptions.contact_list_id','contact_lists.id')
            ->join('languages', 'languages.id', 'contact_list_descriptions.language_id')
            ->select(['contact_lists.*','contact_list_descriptions.title','contact_list_descriptions.address', 'languages.local']);

        $contactListDescription= $query->get();

        foreach ($contactListDescription as $key=>$row)
        {
            $contactList[$row->local] = $row;
        }
        return $contactList;
    }

    public function checkItem(int $id)
    {
        return ContactList::find($id);
    }


    public function phones(int $id)
    {
        return ContactListPhone::where('contact_list_id',$id)->get();
    }

    public function getItemTrashed(int $id)
    {
        $contactList = ContactList::onlyTrashed()->find($id);
        return $contactList ;
    }

    public function create(array $request): ContactList
    {
        $contactList = ContactList::create($request);
        return $contactList;
    }

    public function update(array $request, ?ContactList $contactList): ContactList
    {
        $contactList->update($request);

        return $contactList;
    }

    public function saveData(array $request, int $contactList_id ) :void
    {
        ContactListDescription::where('contact_list_id', $contactList_id)->delete();
        foreach (languages() as $lang)
        {
            $data=[
                'contact_list_id'=> $contactList_id,
                'language_id'=> $lang->id,
                'title'=> $request['title_'.$lang->local],
                'address'=> $request['address_'.$lang->local] ?? '',
            ];
            ContactListDescription::create($data);
        }
    }

    public function saveDataPhone(array $request, int $contactList_id ) :void
    {
        ContactListPhone::where('contact_list_id', $contactList_id)->delete();
        if(isset($request['phones']))
        {
            foreach ($request['phones'] as $phone)
            {
                $data=[
                    'contact_list_id'=> $contactList_id,
                    'phone'=> $phone['phone'] ?? '',
                    // 'country_id'=> $phone['country_id'] ?? '',
                ];
                ContactListPhone::create($data);
            }
        }
    }
    /**
     * Remove the specified Sector from storage.
     * @param ContactList $contactList
     * @return void
     */
    public function destroy(ContactList $contactList) :void
    {
        $contactList->delete();
    }

    /**
     * Remove several contact_lists by IDs.
     *
     * @param array $request
     * @return void
     */
    public function destroyAll(array $ids) :void
    {
        ContactList::whereIn('id', $ids)->delete();
    }

    /**
     * Restore the specified Sector from storage.
     *
     * @param ContactList $contactList
     * @return void
     */
    public function restore(ContactList $contactList) :void
    {
        $contactList->restore();
    }

    /**
     * Restore several contact_lists by IDs.
     *
     * @param array $request
     * @return void
     */
    public function restoreAll(array $ids) :void
    {
        ContactList::whereIn('id', $ids)->onlyTrashed()->restore();
    }
}
