<?php

/**
 * ExecutionController.php
 * Copyright (c) 2019 james@firefly-iii.org
 *
 * This file is part of Firefly III (https://github.com/firefly-iii).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

declare(strict_types=1);

namespace FireflyIII\Http\Controllers\RuleGroup;

use Carbon\Carbon;
use Exception;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\SelectTransactionsRequest;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use FireflyIII\TransactionRules\Engine\RuleEngineInterface;
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;

/**
 * Class ExecutionController
 */
final class ExecutionController extends Controller
{
    private readonly AccountRepositoryInterface $repository;
    private readonly RuleGroupRepositoryInterface $ruleGroupRepository;

    /**
     * ExecutionController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->repository          = app(AccountRepositoryInterface::class);
        $this->ruleGroupRepository = app(RuleGroupRepositoryInterface::class);
        $this->middleware(function ($request, $next) {
            app('view')->share('title', (string) trans('firefly.rules'));
            app('view')->share('mainTitleIcon', 'bi-shuffle');
            $this->repository->setUser(auth()->user());
            $this->ruleGroupRepository->setUser(auth()->user());

            return $next($request);
        });
    }

    /**
     * Execute the given rulegroup on a set of existing transactions.
     *
     * @throws Exception
     */
    public function execute(SelectTransactionsRequest $request, RuleGroup $ruleGroup): RedirectResponse
    {
        Log::debug(sprintf('You have selected rule group #%d', $ruleGroup->id));

        // start code
        /** @var User $user */
        $user          = auth()->user();
        $accounts      = implode(',', $request->input('accounts'));
        // create new rule engine:
        $newRuleEngine = app(RuleEngineInterface::class);
        $newRuleEngine->setUser($user);

        // add date operators.
        if (null !== $request->input('start')) {
            $startDate = new Carbon($request->input('start'));
            $newRuleEngine->addOperator(['type' => 'date_after', 'value' => $startDate->format('Y-m-d')]);
        }
        if (null !== $request->input('end')) {
            $endDate = new Carbon($request->input('end'));
            $newRuleEngine->addOperator(['type' => 'date_before', 'value' => $endDate->format('Y-m-d')]);
        }

        // add extra operators:
        $newRuleEngine->addOperator(['type' => 'account_id', 'value' => $accounts]);

        // set rules:
        $rules         = $this->ruleGroupRepository->getActiveRules($ruleGroup);

        $newRuleEngine->setRules($rules);
        $newRuleEngine->fire();
        $resultCount   = $newRuleEngine->getResults();

        // Tell the user that the job is queued
        session()->flash('success', trans_choice('firefly.applied_rule_group_selection', $resultCount, ['title' => $ruleGroup->title]));

        return redirect()->route('rules.index');
    }

    /**
     * Select transactions to apply the group on.
     *
     * @return Factory|View
     */
    public function selectTransactions(RuleGroup $ruleGroup): Factory|\Illuminate\Contracts\View\View
    {
        $subTitle = (string) trans('firefly.apply_rule_group_selection', ['title' => $ruleGroup->title]);

        return view('rules.rule-group.select-transactions', ['ruleGroup' => $ruleGroup, 'subTitle' => $subTitle]);
    }
}
