<?php

namespace tests\Jobs;

use AbuseIO\Jobs\GenerateTicketsGraphPoints;
use AbuseIO\Models\Ticket;
use AbuseIO\Models\TicketGraphPoint;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use tests\TestCase;

class GenerateTicketsGraphPointsTest extends TestCase
{
    use DatabaseTransactions;

    public function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub

        DB::table('tickets')->update(['created_at' => new \Carbon('2 days ago'), 'updated_at' => new \Carbon('2 days ago')]);
    }

    public function testStoreNewTicketDataForYesterday()
    {
        factory(Ticket::class, 10)->create([
            'created_at' => \Carbon::yesterday(),
            'updated_at' => \Carbon::yesterday(),
        ]);
        $job = new GenerateTicketsGraphPoints();
        $job->storeNewTicketDataForYesterday();
        $this->assertEquals(10, $this->countTicketsNewYesterday());
    }

    private function countTicketsNewYesterday()
    {
        return TicketGraphPoint::getNewDataPointsForYesterday()->reduce(function ($sum, $graphPoint) {
            return $sum + (int) $graphPoint->count;
        });
    }

    public function testStoreTouchedTicketsForYesterday()
    {
        factory(Ticket::class, 10)->create([
            'created_at' => new \Carbon('2 days ago'),
            'updated_at' => \Carbon::yesterday(),
        ]);

        $job = new GenerateTicketsGraphPoints();
        $job->storeTouchedDataForYesterday();
        $this->assertEquals(10, $this->countTicketsTouchedYesterday());
    }

    private function countTicketsTouchedYesterday()
    {
        return TicketGraphPoint::getTouchedDataPointsForYesterday()->reduce(function ($sum, $graphPoint) {
            return $sum + (int) $graphPoint->count;
        });
    }
}
