<?php

namespace App\Repositories;

use App\Models\RegistrationField;

class RegistrationFieldRepository
{
    public function __construct(protected RegistrationField $model) {}

    public function getAll()
    {
        return $this->model->orderBy('sort_order')->orderBy('id')->get();
    }

    public function getActive()
    {
        return $this->model->active()->get();
    }

    public function find($id)
    {
        return $this->model->findOrFail($id);
    }

    public function create(array $data)
    {
        return $this->model->create($data);
    }

    public function update(array $data, $id)
    {
        $field = $this->find($id);
        $field->update($data);
        return $field;
    }

    public function delete($id): void
    {
        $this->find($id)->delete();
    }

    public function reorder(array $order): void
    {
        foreach ($order as $position => $id) {
            $this->model->where('id', $id)->update(['sort_order' => $position]);
        }
    }
}
