<?php

namespace Tests\Http\Controllers\CsvController;

use App\Http\Controllers\CsvController\CsvCostCalc\CsvAgentCost;
use App\Http\Controllers\CsvController\CsvCostCalc\CsvCostCalc;
use App\Http\Controllers\CsvController\CsvCostCalc\CsvMinuteCostCalc;
use App\Http\Controllers\CsvController\CsvDataHandling;
use Tests\TestCase;

class CsvDataHandlingTest extends TestCase
{
protected function setUp(): void
    {parent::setUp();

        $this->mockCsvMinuteCostCalc = $this->createMock(CsvMinuteCostCalc::class);
        $this->csv_data_handling = new CsvDataHandling();
        $this->csv_data_handling->csv_cost_calc->CsvTotalCostCalc->CsvMinuteCostCalc = $this->mockCsvMinuteCostCalc;
        $this->csv_data_handling->csv_cost_calc->CsvAgentCost->CsvMinuteCostCalc = $this->mockCsvMinuteCostCalc;
        $this->csv_data_handling->calculate_agent_cost = true; // Example percentage
        $this->csv_data_handling->csv_cost_calc->CsvAgentCost->internal_percentage_to_deduct = 10; // Example percentage
    }

    public function testHandleAgentIssue()
    {
        // Arrange
        $agents_issue_time = [];
        $time = ['user_name' => 'agent1', 'time' => 5];

        // Act
        $result = $this->csv_data_handling->handle_agent_issue($agents_issue_time, $time);

        // Assert
        $this->assertEquals(['agent1' => 5], $result);

        // Act with another entry for the same agent
        $time = ['user_name' => 'agent1', 'time' => 3];
        $result = $this->csv_data_handling->handle_agent_issue($result, $time);

        // Assert
        $this->assertEquals(['agent1' => 8], $result);

        // Act with a different agent
        $time = ['user_name' => 'agent2', 'time' => 10];
        $result = $this->csv_data_handling->handle_agent_issue($result, $time);

        // Assert
        $this->assertEquals(['agent1' => 8, 'agent2' => 10], $result);
    }

    public function testGetIssueTotalTime()
    {
        // Arrange
        $issue_time = [
            ['user_name' => 'agent1', 'time' => 5],
            ['user_name' => 'agent1', 'time' => 3],
            ['user_name' => 'agent2', 'time' => 10],
        ];

        // Act
        list($total_time, $agents_issue_time) = $this->csv_data_handling->get_issue_total_time($issue_time);

        // Assert
        $this->assertEquals(18, $total_time);
        $this->assertEquals(['agent1' => 8, 'agent2' => 10], $agents_issue_time);
    }

    public function testHandle_csv_time()
    {

        $array = [
            'Progetto' => ' test',
            '#' => 169,
            'Titolo' => 'Test',
            'URL' => ' https://git.test.test/test/issues/169',
            'Aperto_il' => ' 2024-07-19T10:43:07+02:00',
            'Chiuso_il' => ' 2024-07-31T09:50:32+02:00',
            'Etichette' => ' Priority/Medium,RequestBy/Customer,Status/Resolved,',
            'Kind' => ',',
            'Request By' => 'RequestBy/Client',
            'Priority' => 'Priority/High',
        ];

        $issue = [];

        $issue_time = [
            [
                'id' => 1,
                'created' => "2024-07-24T18:48:42+02:00",
                'time' => 600,
                'user_id' => 8,
                'user_name' => "Agent1",
                'issue_id' => 1,
            ],
            [
                'id' => 2,
                'created' => "2024-07-24T18:48:42+02:00",
                'time' => 480,
                'user_id' => 2,
                'user_name' => "Agent2",
                'issue_id' => 2,
            ],
        ];

        $company_agents = ["Agent1", "Agent2"];

        $expected = array_merge($array, [
                'Tempo totale' => '00:18:00',
                'Costo totale' => 180.00,
                'Agent1' => '00:10:00',
                'Agent1 costo' => 90.00,
                'Agent2' => '00:08:00',
                'Agent2 costo' => 72.00,
            ]
        );


        $this->mockCsvMinuteCostCalc
            ->method('select_correct_cost')
            ->with('RequestBy/Client', 'Priority/High')
            ->willReturn(10); // Mocked return value


        // Act
        $result = $this->csv_data_handling->handle_csv_time($array, $issue, $issue_time, $company_agents);

        // Assert
        $this->assertEquals($expected, $result);
    }
}