<?php

namespace App\Classes\Modules\Companies\Standards\Validators;

use App\Classes\General\Abstracts\AbstractValidation;
use App\Classes\Modules\Companies\DataTransferObjects\CompanySegmentObject;
use App\Models\SegmentCompany;
use Illuminate\Support\Facades\Log;

class CompanyToSegmentValidation extends AbstractValidation
{
    /**
     * @param CompanySegmentObject $object
     * @return array
     */
    protected function data($object): array
    {
        return[
            'company_segment' => $object
        ];
    }

    /**
     * @return array
     */
    protected function rules(): array
    {
        return [
            'company_segment' => [
                'required',
                function ($attribute, $obj, $fail) {
                    // $result = SegmentCompany::where('segment_id', $obj->getSegment()->id)->where('company_id', $obj->getCompany()->id)->first();
                    $result = $obj->getCompany()->segments()->where('segment_id', $obj->getSegment()->id)->first();
                    Log::info('SegmentCompany: '.json_encode($result));
                    if (!is_null($result)) {
                        $fail('The segment company already exists');
                    }
                },
            ]
        ];
    }

    /**
     * @return array
     */
    protected function messages(): array
    {
        return [];
    }

}
