<?php

namespace App\Repositories;

use App\Models\Producer;

class ProducerRepository implements ProducerRepositoryInterface
{
    /**
     * Get's a record by it's ID
     *
     * @param int
     */
    public function get($id): array
    {
        return Producer::find($id)->toArray();
    }

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

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

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

    /**
     * Get's all records by indexed.
     *
     * @return mixed
     */
    public function allByIndex($index)
    {
        if (! $index) {
            return Producer::all()->keyBy('id')->toArray();
        } else {
            return Producer::all()->keyBy($index)->toArray();
        }
    }

    /**
     * create new Record
     *
     * @param array
     * @return collection|bool
     */
    public function add($data)
    {

        $producer = new Producer;
        $producer->fill($data);
        if ($producer->save()) {
            return $producer->id;
        } else {
            return false;
        }

    }
}
