<?php

namespace App\Repositories;

use App\Models\Address;
use App\Models\Contact;
use App\Traits\RepositoryRelationshipsTraits;

class AddressRepository implements AddressRepositoryInterface
{
    use RepositoryRelationshipsTraits;

    private string $Model;

    private array $defaultRelations = [];

    public function __construct()
    {
        $this->Model = Address::class;
        $this->actualRelations = $this->defaultRelations;
    }

    /**
     * Get's a record by it's ID
     *
     * @param int
     */
    public function get($id)
    {
        return Contact::find($id);
    }

    /**
     * Get's all records.
     *
     * @return mixed
     */
    public function all()
    {
        return Contact::all();
    }

    /**
     * Deletes a record.
     *
     * @param int
     */
    public function delete($id)
    {
        Contact::destroy($id);
    }

    /**
     * Updates a post.
     *
     * @param int
     * @param array
     */
    public function update($id, array $data)
    {
        Contact::find($id)->update($data);
    }

    public function add($data): ?int
    {
        $obj = new $this->Model;
        $obj->canSee = true;
        $obj->status == \App\Enums\DbStatusFieldEnum::draft;
        $obj->fill($data);
        $obj->save();

        return $obj->id;
    }

    public function associate($id, $relationId, $relationClass): bool
    {
        if (! $item = $this->Model::find($id)) {
            return false;
        }
        if (! $attachedItem = $relationClass::find($relationId)) {
            return false;
        }

        $ret = false;
        if ($item->attachable()->associate($attachedItem)) {
            if ($item->save()) {
                $ret = true;
            }
        }

        return $ret;
    }

    public function dissociate($id, $relationId, $relationClass): bool
    {
        if (! $item = $this->Model::find($id)) {
            return false;
        }
        if (! $attachedItem = $relationClass::find($relationId)) {

        }
        $ret = false;
        if ($item->attachable()->dissociate($attachedItem)) {
            if ($item->save()) {
                $ret = true;
            }
        }

        return $ret;
    }

    public function setRelationItem($id, $relationId, $relationType): bool
    {
        if (! $item = $this->Model::find($id)) {
            return false;
        }

        if (! $attachedItem = $relationType::find($relationId)) {
            return false;
        }

        $ret = false;
        if ($item->attachable()->associate($attachedItem)) {
            if ($item->save()) {
                $ret = true;
            }
        }

        return $ret;
    }

    public function unsetRelationItem($id, $relationId, $relationType): bool
    {
        if (! $item = $this->Model::find($id)) {
            return false;
        }

        if (! $attachedItem = $relationType::find($relationId)) {
            return false;
        }

        $ret = false;
        if ($item->attachable()->dissociate($attachedItem)) {
            if ($item->save()) {
                $ret = true;
            }
        }

        return $ret;
    }
}
