<?php

namespace Bixo\Niche\Actions;

use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Litepie\Actions\Concerns\AsAction;
use Bixo\Niche\Models\NicheLocation;


class NicheLocationAction
{
    use AsAction;

    protected $model;
    protected $namespace = 'bixo.niche.niche_location';
    protected $eventClass = \Bixo\Niche\Events\NicheLocationAction::class;
    protected $action;
    protected $function;
    protected $request;

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

    }


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

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

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

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

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

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

        return $niche_location;
    }


}