<?php

declare(strict_types=1);

namespace app\admin\controller\mall;

use app\common\controller\Backend;
use app\common\library\MallPlayxRatios;
use app\common\model\MallBusinessConfig;
use support\Response;
use Webman\Http\Request;

/**
 * 积分商城参数（PlayX 比例等，读写 mall_business_config）
 */
class PlayxConfig extends Backend
{
    public function index(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) {
            return $response;
        }

        $row = MallBusinessConfig::order('id', 'asc')->find();
        if (!$row) {
            $now = time();
            MallBusinessConfig::create([
                'return_ratio'          => floatval(env('PLAYX_RETURN_RATIO', '0.1')),
                'unlock_ratio'          => floatval(env('PLAYX_UNLOCK_RATIO', '0.1')),
                'points_to_cash_ratio' => floatval(env('PLAYX_POINTS_TO_CASH_RATIO', '0.1')),
                'create_time'           => $now,
                'update_time'           => $now,
            ]);
            $row = MallBusinessConfig::order('id', 'asc')->find();
        }

        if ($row) {
            MallPlayxRatios::syncFromRow($row);
        }

        return $this->success('', [
            'row'    => $row ? $row->toArray() : [],
            'remark' => get_route_remark(),
        ]);
    }

    public function save(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) {
            return $response;
        }

        $data = $request->post();
        if (empty($data) || !is_array($data)) {
            return $this->error(__('Parameter %s can not be empty', ['']));
        }

        $return = $data['return_ratio'] ?? null;
        $unlock = $data['unlock_ratio'] ?? null;
        $cash = $data['points_to_cash_ratio'] ?? null;
        if (!is_numeric($return) || !is_numeric($unlock) || !is_numeric($cash)) {
            return $this->error(__('Parameter error'));
        }

        $returnF = floatval($return);
        $unlockF = floatval($unlock);
        $cashF = floatval($cash);
        if ($returnF < 0 || $unlockF < 0 || $cashF < 0) {
            return $this->error(__('Parameter error'));
        }

        $row = MallBusinessConfig::order('id', 'asc')->find();
        if (!$row) {
            $now = time();
            MallBusinessConfig::create([
                'return_ratio'          => $returnF,
                'unlock_ratio'          => $unlockF,
                'points_to_cash_ratio' => $cashF,
                'create_time'           => $now,
                'update_time'           => $now,
            ]);
        } else {
            $row->return_ratio = $returnF;
            $row->unlock_ratio = $unlockF;
            $row->points_to_cash_ratio = $cashF;
            $row->save();
        }

        $fresh = MallBusinessConfig::order('id', 'asc')->find();
        if ($fresh) {
            MallPlayxRatios::syncFromRow($fresh);
        }

        return $this->success(__('The current page configuration item was updated successfully'));
    }
}
