/**
 * Temporary fix for StockReportsCtrl infinite loop issue
 * This file overrides the problematic controller until build system is fixed
 */

// Override the StockReportsCtrl with our fixed version
if (typeof StockModule !== 'undefined') {
    StockModule.controller('StockReportsCtrl', ['$scope', 'StockReport', 'ErrorsHandler', 'Messenger', '$paginator', '$timeout', function ($scope, StockReport, ErrorsHandler, Messenger, $paginator, $timeout) {

        var search_timeout = 200;
        var isInitialized = false;

        $scope.reports = [];
        $scope.paginator = $paginator;

        //Pagination variables
        $paginator = angular.copy($paginator);
        $scope.paginator = $paginator;
        $paginator.setLimit(10);

        $scope.filters = {
            limit: 10,
            aknowledged: 0,
            type: 'mismatched',
            search: null
        };

        $scope.$watch('activeTab', function (value) {
            if (value == $scope.index && isInitialized) $scope.loadReports();
        });

        $scope.$on('$routeChangeSuccess', function (ev, currentRoute, previousRoute) {
            // Only load reports if we're actually on the stock reports route
            if (currentRoute && currentRoute.$$route && currentRoute.$$route.originalPath === '/stock/stock_reports/') {
                $scope.loadReports();
            }
        });

        $paginator.setOnPageChange(function (page) {
            $scope.loadReports(page);
        });

        $paginator.setOnLimitChange(function (page) {
            $scope.loadReports(page);
        });

        $scope.$watch('filters.limit', function (newValue, oldValue) {
            if (newValue !== oldValue && isInitialized)
                $paginator.setLimit($scope.filters.limit);
        });

        $scope.$watch('filters.search', function (newValue, oldValue) {
            if (newValue === oldValue || !isInitialized) return;
            if (search_timeout) $timeout.cancel(search_timeout);

            search_timeout = $timeout(function () {
                $scope.products = $scope.loadReports();
            }, 250);
        });

        $scope.$watch('filters.aknowledged', function (newValue, oldValue) {
            if (newValue !== oldValue && isInitialized) {
                $scope.loadReports();
            }
        });

        $scope.$watch('filters.type', function (newValue, oldValue) {
            if (newValue !== oldValue && isInitialized) {
                $scope.loadReports();
            }
        });

        $scope.loadReports = function (page) {
            var data = {
                aknowledged: $scope.filters.aknowledged,
                type: $scope.filters.type,
                query: $scope.filters.search,
                page: page
            };

            StockReport.get(data, function (response) {

                $scope.loading = false;
                $scope.reports = response.data;
                $paginator.setFromResponse(response);

            }, ErrorsHandler);


        };

        $scope.markAsAknowledged = function (report) {

            var data = {
                id: report.id,
                aknowledged: 1
            };

            StockReport.update(data, function (response) {
                report.aknowledged = 1;
                Messenger('Report marked as aknowledged successfully!');

            });
        };

        // Load reports once and mark as initialized
        $scope.loadReports();
        isInitialized = true;

    }]);

    console.log('🔧 StockReportsCtrl fix applied - infinite loop should be resolved');
} 