<?php

namespace App\Http\Controllers\APIS\AdminApi;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\RuleChainService;

use Exception;

class RuleChainController extends Controller
{
    private $ruleChainService;

    public function __construct(RuleChainService $ruleChainService)
    {
        $this->ruleChainService = $ruleChainService;
    }



    public function getRuleChainList(Request $request)
    {
        try {
            $page = $request->get('page', 0);            // Default: first page
            $pageSize = $request->get('pageSize', 10);   // Default: 10 records per page

            $data = $this->ruleChainService->getRuleChains($page, $pageSize);
            if (!empty($data['data'])) {
                return jsonResponseWithSuccessMessage('Rule chains fetched successfully', $data);
            } else {
                return jsonResponseWithErrorMessage('No rule chains found', 404);
            }
        } catch (Exception $e) {
            return jsonResponseWithErrorMessage($e->getMessage(), 500);
        }
    }

    public function exportruleChain(Request $request)
    {
        try {
            $limit = $request->get('limit', 10000);  // Default limit: 100

            $data = $this->ruleChainService->exportRuleChains($limit);

            if (!empty($data['ruleChains'])) {
                return jsonResponseWithSuccessMessage('Rule chains exported successfully', $data);
            } else {
                return jsonResponseWithErrorMessage('No rule chains found', 404);
            }

        } catch (Exception $e) {
            return jsonResponseWithErrorMessage($e->getMessage(), 500);
        }
    }

    public function showruleChain($ruleChainId)
    {
        try {
            $data = $this->ruleChainService->getRuleChainById($ruleChainId);

            if (!empty($data)) {
                return jsonResponseWithSuccessMessage('Rule chain fetched successfully', $data);
            } else {
                return jsonResponseWithErrorMessage('No rule chain found', 404);
            }

        } catch (Exception $e) {
            return jsonResponseWithErrorMessage($e->getMessage(), 500);
        }
    }


}
