<?php

namespace App\State;

use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\Pagination\Pagination;
use ApiPlatform\State\Pagination\TraversablePaginator;
use ApiPlatform\State\ProviderInterface;
use App\ApiResource\SaleSummary;
use App\ApiResource\UserApi;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfonycasts\MicroMapper\MicroMapperInterface;

class SaleSummaryStateProvider implements ProviderInterface
{
    public function __construct(
        private UserRepository$userRepository,
        private Pagination $pagination,
        private MicroMapperInterface $microMapper,
    )
    {
    }

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
    {
        if ($operation instanceof CollectionOperationInterface) {
            $currentPage = $this->pagination->getPage($context);
            $itemsPerPage = $this->pagination->getLimit($operation, $context);
            $offset = $this->pagination->getOffset($operation, $context);
            $salesSummaries = $this->createSaleSummaries($offset, $itemsPerPage);

            return new TraversablePaginator(
                new \ArrayIterator($salesSummaries),
                $currentPage,
                $itemsPerPage,
                count($salesSummaries),
            );
        }

        $salesSummaries = $this->createSaleSummaries(0);

        return $this->createSaleSummaries(0, count($salesSummaries));
    }

    private function createSaleSummaries(int $offset, int $limit = 50): array
    {
        $users = $this->userRepository->findBy([], [], $limit, $offset);
        $salesSummaries = [];
        /** @var User $user */
        foreach ($users as $user) {
            $sales = $user->getSales();
            if (count($sales) > 0) {
                $salesSummary = new SaleSummary();
                $salesSummary->owner = $this->microMapper->map($user, UserApi::class);
                $salesSummary->ownerName = $user->getFirstName() . ' ' . $user->getLastName();
                $salesSummary->turnover = 0;
                $salesSummary->profit = 0;
                foreach ($sales as $sale) {
                    $salesSummary->turnover += $sale->getTurnover();
                    $salesSummary->profit += $sale->getProfit();
                }
                $salesSummaries[] = $salesSummary;
            }

        }
        usort($salesSummaries, function(SaleSummary $a, SaleSummary $b) {
            return $b->turnover - $a->turnover;
        });
        return $salesSummaries;
    }
}
