<?php

namespace App\Http\Controllers\Web\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Web\Staking\StakingFormRequest;
use App\Models\Staking\Staking;
use App\Repositories\Staking\StakingRepository;
use App\Services\Currency\CurrencyService;
use Illuminate\Support\Facades\Redirect;
use Inertia\Inertia;
use Setting;

class StakingController extends Controller
{
    /**
     * @var StakingRepository
     */
    protected $stakingRepository;

    /**
     * StakingController Constructor
     *
     * @param StakingRepository $stakingRepository
     *
     */
    public function __construct(StakingRepository $stakingRepository)
    {
        $this->stakingRepository = $stakingRepository;
    }


    public function index()
    {
        $stakings = $this->stakingRepository->get();

        return Inertia::render('Admin/Stakings/Index', [
            'stakings' => $stakings,
        ]);
    }

    /**
     * Create new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $currencies = (new CurrencyService())->getCurrencies(false, true);

        return Inertia::render('Admin/Stakings/Form', [
            'currencies' => $currencies,
        ]);
    }

    /**
     * Store new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(StakingFormRequest $request)
    {
        $this->stakingRepository->store($request->only([
            'currency_id',
            'allowed_days',
            'rewards_percentage',
            'min_amount',
            'max_amount',
            'status'
        ]));

        return Redirect::route('admin.stakings');
    }

    /**
     * Edit resource.
     *
     * @param Staking $staking
     * @return \Inertia\Response
     */
    public function edit(Staking $staking)
    {
        $currencies = (new CurrencyService())->getCurrencies(false, true);

        $staking = $this->stakingRepository->getStakingById($staking->id);

        return Inertia::render('Admin/Stakings/Form', [
            'isEdit' => true,
            'staking' => $staking,
            'currencies' => $currencies,
        ]);
    }

    /**
     * Update resource.
     *
     * @param Staking $staking
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(StakingFormRequest $request, Staking $staking)
    {
        $this->stakingRepository->update($staking->id, $request->only([
            'currency_id',
            'allowed_days',
            'rewards_percentage',
            'min_amount',
            'max_amount',
            'status'
        ]));

        return Redirect::route('admin.stakings');
    }

    /**
     * Destroy resource.
     *
     * @param Staking $staking
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(Staking $staking)
    {
        $this->stakingRepository->delete($staking->id);

        return Redirect::route('admin.stakings');
    }
}
