<?php

namespace App\Http\Controllers\GiteaApiController;

use App\Http\Controllers\Controller;
use App\Http\Controllers\CsvController\CsvController;
use App\Http\Controllers\CsvController\CsvCostCalc\CsvCostCalc;
use App\Http\Controllers\IssueValidationController;
use App\Http\Controllers\ThirdPartyServices\ThirdPartyServices;
use OwenVoke\Gitea\Client;

class GiteaExport extends Controller
{
    private $giteaClient;

    private $issue_validation_controller;

    private $gitea_fetch;

    private $csv_controller;

    private $third_party_integrations_allow;
    private $calculate_agent_cost;
    private $csv_cost_calc;
    private $third_party_services;

    private $email_send_allow;
    public function __construct()
    {
        $this->third_party_integrations_allow = env('THIRD_PARTY_INTEGRATIONS_ALLOW', false);
        $this->calculate_agent_cost = env('GITEA_CALCULATE_AGENT_COST', false);
        $this->email_send_allow = env('EMAIL_SEND_ALLOW', false);
        $this->email_address = env('EMAIL_ADDRESS', false);
        $this->email_prefix_subject = env('EMAIL_PREFIX_SUBJECT', false);
        $this->giteaClient = new Client(null, null, getenv('GITEA_URL'));
        $this->giteaClient->authenticate(getenv('GITEA_TOKEN'), null, Client::AUTH_ACCESS_TOKEN);
        $this->issue_validation_controller = new IssueValidationController();
        $this->csv_controller = new CsvController();
        $this->gitea_fetch = new GiteaFetch();
        $this->csv_cost_calc = new CsvCostCalc();
        $this->third_party_services = new ThirdPartyServices();
    }

    private function handle_repos(string $from_date, string $to_date, array $repositories, array $issues_params){
        $data = [];
        foreach ($repositories as $repository) {
            $issues = $this->gitea_fetch->get_issues($repository['name'], $issues_params);
            foreach ($issues as $issue) {
                $issue_time = $this->gitea_fetch->get_issue_time($repository['name'], $issue['number']);
                $issue_valid = $this->issue_validation_controller->handle_single_issue($from_date, $to_date, $issue, $issue_time);
                if ($issue_valid) {
                    $company_agents = $this->calculate_agent_cost ? $this->gitea_fetch->get_company_agents() : [];
                    $data[] = $this->csv_controller->create_columns($issue, $issue_time, $company_agents);
                }
            }
        }
        return $data;
    }


    function export_issues(string $from_date, string $to_date, array $issues_params)
    {
        $repositories = $this->gitea_fetch->get_repositories();
        $data = $this->handle_repos($from_date,  $to_date, $repositories, $issues_params);
        if (count($data) > 0) {
            $company_agents = $this->calculate_agent_cost ? $this->gitea_fetch->get_company_agents() : [];
            $data = $this->csv_cost_calc->sum_costs($data, $company_agents);
            $csv_file = $this->csv_controller->create_csv($issues_params['state'], $from_date, $to_date, $data);
            if($this->third_party_integrations_allow) {
                $this->third_party_services->handle_third_party_services($csv_file['file_name'], $csv_file['file_path'], $data, $company_agents, $from_date, $to_date);
            }
            isset($csv_file['file_path']) ?? unlink($csv_file['file_path']);
            readfile($csv_file['file_path']);
            unlink($csv_file['file_path']);
            return true;
        }
        return false;
    }
}