<?php

namespace App\Services;

use App\Models\Role;
use App\Models\User;
use App\Repositories\ProfitCenterRepository;

class ProfitCenterService extends ServiceBase
{
    public function __construct(ProfitCenterRepository $Repository)
    {
        parent::__construct();
        $this->Repository = $Repository;
        // $this->setRelation('contact','productGroup');
    }

    public function addContact($id, $contactIds = []): bool
    {
        return $this->Repository->addContact($id, $contactIds);
    }

    public function removeContact($id, $contactIds = []): bool
    {
        return $this->Repository->removeContact($id, $contactIds);
    }

    public function syncContact($id, $contactIds = []): bool
    {
        return $this->Repository->syncContact($id, $contactIds);
    }

    public function syncRelationItem($id, $relIds, $relType): bool
    {

        return $this->Repository->syncRelationItem($id, $relIds, $relType);
    }

    public function addRelationItem($id, $relIds, $relType): bool
    {
        return $this->Repository->addRelationItem($id, $relIds, $relType);
    }

    public function unsetRelationItem($id, $relIds, $relType): bool
    {
        return $this->Repository->unsetRelationItem($id, $relIds, $relType);
    }

    public function syncUser($id, $userData)
    {
        if (! $userData['name']) {
            return false;
        }
        $this->Repository->setRelation('users');
        // $itemData=$this->Repository->getWithRelations($id);

        $userPass = null;
        if ($userData['password']) {
            if ($userData['password'] !== $userData['repassword']) {
                $this->error->add('password', 'Nem egyeznek a jeszavak');

                return false;
            }
            $userPass = \Hash::make($userData['password']);
        }
        /*
         * letezo
         * - valtozott az user data, modositani kell
         * - nem valtozott az user data
         * Nem letezo
         * - fel kell venni
         */
        if ($userData['id'] && $userData['id'] != '') {
            $user = User::find($userData['id']);
            if ($userPass) {
                $user->password = $userPass;
            }
            if ($user->name !== $userData['name']) {
                $user->name = $userData['name'];
            }
            $user->save();
        } else {
            /*Mi a tokomert nem kerul ki a debugba az uzenet???
                es miert true a visszateres???*/
            $user = User::Create([
                'name' => $userData['name'],
                // 'password'=>$userPass,
                'password' => \Hash::make($userData['password']),
                'email' => 'profitCenter'.time().'@eurest.hu',
            ]);

            $RoleProfitCenter = Role::where('name', 'profit-center')->first();
            $user->syncRolesWithoutDetaching([$RoleProfitCenter]);
        }

        return $this->Repository->syncUser($id, $user->id);

    }

    public function syncSupplier($id, $supplierId = []): bool
    {
        return $this->Repository->syncSupplier($id, $supplierId);
    }

    public function add($data): ?int
    {
        $contactIds = $data['contactId'];
        $addressId = $data['addressId'];
        $user = $data['user'];
        unset($data['contactId'],$data['addressId']);
        if (! $id = parent::add($data)) {
            return false;
        }
        $this->addContact($id, $contactIds);
        $this->addRelationItem($id, $addressId, 'Address');
        if (! $this->syncUser($id, $user)) {
            return false;
        }

        return $id;
    }

    /**
     * Updates a post.
     *
     * @param int
     * @param array
     */
    public function update($id, array $data): bool
    {
        $contactIds = $data['contactId'];
        $addressId = $data['addressId'];
        $supplierId = $data['supplierId'];
        $user = $data['user'];
        unset($data['contactId'],$data['addressId'],$data['user']);
        if ($this->Repository->update($id, $data)) {
            $this->syncContact($id, $contactIds);
            $this->syncRelationItem($id, $addressId, 'Address');
            $this->syncSupplier($id, $supplierId);
            if (! $this->syncUser($id, $user)) {
                return false;
            }

            return true;
        }

        return false;
    }
}
