<?php

namespace Litecms\Location\Actions;

use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Litepie\Actions\Concerns\AsAction;
use Litepie\Actions\Traits\LogsActions;
use Litepie\Notification\Traits\SendNotification;
use Litecms\Location\Models\Location;


class LocationAction
{
    use AsAction;

    protected $model;
    protected $namespace = 'litecms.location.location';
    protected $eventClass = \Litecms\Location\Events\LocationAction::class;
    protected $action;
    protected $function;
    protected $request;

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

    }


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

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

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

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

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

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

        return $location;
    }


}