<?php

// src/Controller/ProductController.php

namespace App\Service;

use Doctrine\DBAL\DBALException;
use Doctrine\ORM\ORMException;
use PDOException;
use Exception;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Uid\Uuid;
use Doctrine\DBAL\Connection;
use App\Entity\Location;
use League\Csv\Reader;

//use App\Utils\StringTools;

/**
 * Members
 *
 * Data class for importing external CVS blocklists
 */
class HandleImports
{
    private $session;
    private $entityManager;
    private $conn;
    private $limit = 4;

    public function __construct(
        Connection $connection,
        EntityManagerInterface $entityManager,
        RequestStack $requestStack
    ) {
        $this->connection    = $connection;
        $this->entityManager = $entityManager;
        $this->session       = $requestStack->getSession();
    }

    /**
     * Add new Locations to db from fedifence file
     * provided by Oliphant
     *
     * @param string $path location of file
     * @param int $memberID member adding locations
     * @return Object
     */
    public function importLocations($file, $memberId)
    {
        //read csv
        $csv = Reader::createFromPath($file, "r");
        $csv->setHeaderOffset(0);
        $records      = $csv->getRecords();
        $recordCount  = count($csv);
        $duplicates   = 0;
        $errorMessage = null;

        //extract data row by row

        //TODO: set name to lowercase for comparison
        foreach ($records as $offset => $row) {
            $url = $row["domain"];
            //$images   = $row["Images"];
            $desc    = $row["public_comment"];
            $ratings = $row["severity"];

            //check to see if location already exists
            $list  = $this->entityManager->getRepository(Location::class);
            $entry = $list->findOneBy(["url" => trim($url)]);
            if ($entry) {
                ++$duplicates;
            } else {
                $errorMessage = null;
                $location     = new Location();

                $location->setName($url);
                $location->setUrl($url);
                $location->setTags("bigotry, hate speech, poor moderation");
                if ($ratings == "suspend") {
                    $location->setRating("defederate");
                } else {
                    $location->setRating("silence");
                }

                //set defaults
                $location->setUuid(Uuid::v4());
                if ($desc == "") {
                    $location->setDescription("needs description");
                    $location->setActive(false);
                } else {
                    $location->setActive(true);
                    $location->setDescription($desc);
                }

                $location->setCreatedAt(new \DateTimeImmutable());
                $location->setUpdatedAt(new \DateTimeImmutable());
                $location->setAddedBy($memberId);

                $this->entityManager->persist($location);
            }
        }

        try {
            $this->entityManager->flush();
        } catch (PDOException $error) {
            $errorMessage = $error->getMessage();
        } catch (DBALException $error) {
            $errorMessage = $error->getMessage();
        } catch (ORMException $error) {
            $errorMessage = $error->getMessage();
        } catch (Exception $error) {
            $errorMessage = $error->getMessage();
        } catch (SyntaxErrorException $e) {
            $errorMessage = $error->getMessage();
        }

        if ($duplicates > 0) {
            $message = $duplicates . " of " . $recordCount . " location entries  were duplicates";
        } else {
            $message = "Locations Added. Nice Job!";
        }
        // return result status
        if ($errorMessage == null) {
            return $response = [
                "status"  => true,
                "message" => $message,
            ];
        } else {
            return $response = ["status" => false, "message" => $errorMessage];
        }
    }
}
