<?php

namespace Bixo\Deal\Actions;

use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Litepie\Actions\Concerns\AsAction;
use Bixo\Deal\Models\Payment;


class PaymentAction
{
    use AsAction;

    protected $model;
    protected $namespace = 'bixo.deal.payment';
    protected $eventClass = \Bixo\Deal\Events\PaymentAction::class;
    protected $action;
    protected $function;
    protected $request;

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

    }


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

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

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

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

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

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

        return $payment;
    }


}