<?php

namespace Modules\Company\Repositories;

use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Company\Interfaces\CompanyRepositoryInterface;
use Modules\Company\Models\Company;
use Modules\Company\Models\CompanyDescription;

class CompanyRepository implements CompanyRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Company::query()
            ->join('company_descriptions AS sd', 'companies.id', 'sd.company_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title','sd.description','sd.address', 'companies.*']);
        return $builder;
    }

    public function getCompanyDescription(array $request)
    {
        return CompanyDescription::query();
    }

    public function getItem(int $id)
    {
        $company = Company::find($id);

        $query = CompanyDescription::where('company_id', $company->id)
            ->join('companies', 'company_descriptions.company_id','companies.id')
            ->join('languages', 'languages.id', 'company_descriptions.language_id')
            ->select(['companies.*','company_descriptions.title','company_descriptions.description','company_descriptions.address', 'languages.local']);

        $companyDescription= $query->get();

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

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

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

    public function create(array $request): Company
    {
        $company = Company::create($request);
        return $company;
    }

    public function update(array $request, ?Company $company): Company
    {
        $company->update($request);

        return $company;
    }

    public function saveData(array $request, int $company_id ) :void
    {
        CompanyDescription::where('company_id', $company_id)->delete();
        foreach (languages() as $lang)
        {
            $data=[
                'company_id'=> $company_id,
                'language_id'=> $lang->id,
                'title'=> $request['title_'.$lang->local],
                'description'=> $request['description_'.$lang->local] ?? null,
                'address'=> $request['address_'.$lang->local] ?? null,
            ];
            CompanyDescription::create($data);
        }
    }
    /**
     * Remove the specified Sector from storage.
     * @param Company $company
     * @return void
     */
    public function destroy(Company $company) :void
    {
        $company->delete();
    }

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

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

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