<?php

namespace Realestate\Neighbourhood\Actions;

use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Litepie\Actions\Concerns\AsAction;
use Realestate\Neighbourhood\Models\Neighbourhood;


class NeighbourhoodAction
{
    use AsAction;

    protected $model;
    protected $namespace = 'realestate.neighbourhood.neighbourhood';
    protected $eventClass = \Realestate\Neighbourhood\Events\NeighbourhoodAction::class;
    protected $action;
    protected $function;
    protected $request;

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

    }


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

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

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

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

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

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

        return $neighbourhood;
    }


}