<?php

namespace Eform\Instruction\Actions;

use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Litepie\Actions\Concerns\AsAction;
use Eform\Instruction\Models\InstructionCategory;


class InstructionCategoryAction
{
    use AsAction;

    protected $model;
    protected $namespace = 'eform.instruction.instruction_category';
    protected $eventClass = \Eform\Instruction\Events\InstructionCategoryAction::class;
    protected $action;
    protected $function;
    protected $request;

    public function handle(string $action, InstructionCategory $instruction_category, array $request = [])
    {
        $this->action = $action;
        $this->request = $request;
        $this->model = $instruction_category;
        $this->function = Str::camel($action);
        $this->executeAction();
        return $this->model;

    }


    public function store(InstructionCategory $instruction_category, array $request)
    {
        $attributes = $request;
        $attributes['user_id'] = user_id();
        $attributes['user_type'] = user_type();
        $instruction_category = $instruction_category->create($attributes);
        return $instruction_category;
    }

    public function update(InstructionCategory $instruction_category, array $request)
    {
        $attributes = $request;
        $instruction_category->update($attributes);
        return $instruction_category;
    }

    public function destroy(InstructionCategory $instruction_category, array $request)
    {
        $instruction_category->delete();
        return $instruction_category;
    }

    public function copy(InstructionCategory $instruction_category, array $request)
    {
        $count = $request['count'] ?: 1;

        if ($count == 1) {
            $instruction_category = $instruction_category->replicate();
            $instruction_category->created_at = Carbon::now();
            $instruction_category->save();
            return $instruction_category;
        }

        for ($i = 1; $i <= $count; $i++) {
            $new = $instruction_category->replicate();
            $new->created_at = Carbon::now();
            $new->save();
        }

        return $instruction_category;
    }


}