<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\Models;

use Validator;

/**
 * Description of CompanyModel
 *
 * @author jehm2
 */
class CompanyModel
{

    public static function getCompanies($filters = array(), $paginate = false)
    {
        $query = Company::query();
        return $paginate ? $query->paginate($paginate) : $query->get();
    }

    public static function getCompany($company_id)
    {
        return Company::find($company_id);
    }

    public static function save($data)
    {
        $company_id = $data['company_id'] ?? null;
        $company = CompanyModel::getCompany($company_id);
        if ($company) {
            $company->update($data);
        } else {
            $company = Company::create($data);

            // if company create office
            if ($company) {
                $officeData = array(
                    'company_id' => $company->company_id,
                    'name' => 'Principal',
                );
                $office = OfficeModel::save($officeData);
            }
        }

        return $company;
    }

    public static function delete($company_id)
    {
        $company = CompanyModel::getCompany($company_id);
        if ($company) {
            return $company->delete();
        }
        return false;
    }

    public static function getCompanyQuery($request)
    {
        $query = Company::query();

        if (isset($request->keyword)) {
            $query->where(function ($sQuery) use ($request) {
                $sQuery->where('name', 'like', "%{$request->keyword}%");
                $sQuery->where('nit', 'like', "%{$request->keyword}%");
                $sQuery->where('email', 'like', "%{$request->keyword}%");
                $sQuery->where('phone', 'like', "%{$request->keyword}%");
                $sQuery->where('agentName', 'like', "%{$request->keyword}%");
                $sQuery->where('agentPhone', 'like', "%{$request->keyword}%");
            });
        }

        return $query;
    }

    public static function getValidator($data)
    {

        $company_id = $data['company_id'] ?? null;
        $rules = [
            'name' => ['required', 'max:255'],
            'nit' => ['required', 'string', 'max:100', 'unique:companies,nit,' . $company_id . ',company_id'],
            'email' => ['required', 'email', 'max:255'],
            'phone' => ['max:255'],
            'agentName' => ['max:255'],
            'agentPhone' => ['max:255'],
        ];

        $messages = ModelUtils::getMessages();

        $validator = Validator::make($data, $rules, $messages);

        $niceNames = array(
            'name' => 'Nombre',
            'nit' => 'NIT',
            'email' => 'Email',
            'phone' => 'Teléfono',
            'agentName' => 'Nombre Representante',
            'agentPhone' => 'Teléfono Representante',
        );
        $validator->setAttributeNames($niceNames);

        return $validator;
    }
}
