<?php

namespace KupShop\ReclamationsBundle\Inspections\User;

use KupShop\SystemInspectionBundle\Inspections\Inspection;
use KupShop\SystemInspectionBundle\Inspections\User\UserInspectionInterface;
use KupShop\SystemInspectionBundle\InspectionWriters\MessageTypes\InspectionMessage;
use KupShop\SystemInspectionBundle\InspectionWriters\MessageTypes\StuckOrdersMessage;

class ReclamationsNearDeadline extends Inspection implements UserInspectionInterface
{
    /**
     * @return InspectionMessage[]|null
     */
    public function runInspection(): ?array
    {
        $errors = [];
        $qb = sqlQueryBuilder()->addCalcRows()->addselect('code')->from('reclamations')
            ->where('date_accepted < NOW() - INTERVAL 27 DAY')
            ->andWhere('status = 1')
            ->setMaxResults(5);

        $oldOrders = sqlFetchAll($qb, 'code');

        if ($oldOrders) {
            $count = sqlQuery('SELECT FOUND_ROWS()')->fetchColumn();
            $errors[] = new StuckOrdersMessage(translate('nearDeadline', 'Reclamations', false, true), array_keys($oldOrders), $count);
        }

        return $errors;
    }
}
