<?php

namespace App\Service;

use Symfony\Component\HttpFoundation\Response;
use League\Csv\Reader;

class HandleExports
{
    private $locations;

    public function __construct(HandleLocations $locations)
    {
        $this->locations = $locations;
    }

    public function buildMastodonCSV()
    {
        $records = [['domain', 'severity', 'public_comment', 'reject_media', 'reject_reports', 'obfuscate']];
        $entries = $this->locations->getActiveLocations();
        foreach ($entries as $entry) {
            $item = [$entry->getUrl(), $entry->getRating(), $entry->getDescription(), "FALSE", "FALSE", "FALSE"];
            array_push($records, $item);
        }

        $tmp = new \SplTempFileObject();
        foreach ($records as $record) {
            $tmp->fputcsv($record);
        }

        $reader = Reader::createFromFileObject($tmp);
        return new Response($reader->getContent(), 200, [
            'Content-Encoding'    => 'none',
            'Content-Type'        => 'text/csv; charset=UTF-8',
            'Content-Disposition' => 'attachment; filename="the-bad-space-mastodon.csv"',
            'Content-Description' => 'File Transfer',
        ]);

        //return $path;
    }
}
