<?php

namespace App\Http\Controllers\ThirdPartyServices;

use App\Http\Controllers\Controller;
use App\Http\Controllers\ThirdPartyServices\NextcloudService;
use App\Http\Controllers\ThirdPartyServices\OpenProjectService;
use App\Http\Controllers\ThirdPartyServices\EmailService;

class ThirdPartyServices extends Controller
{
    private $third_party_integrations_nextcloud;
    private $third_party_integrations_open_project;

    public function __construct($third_party_integrations_nextcloud, $third_party_integrations_open_project, $email_send_allow)
    {
        $this->third_party_integrations_nextcloud = $third_party_integrations_nextcloud || config('app.third_party_integrations_nextcloud', false);
        $this->third_party_integrations_open_project = $third_party_integrations_open_project || config('app.third_party_integrations_open_project', false);
        $this->email_send_allow = $email_send_allow || config('app.email_send_allow', false);
        $this->nextcloud_service = new \App\Http\Controllers\ThirdPartyServices\NextcloudService();
        $this->open_project_service = new \App\Http\Controllers\ThirdPartyServices\OpenProjectService();
        $this->email_service = new \App\Http\Controllers\ThirdPartyServices\EmailService();
        $this->calculate_agent_cost = config('app.gitea_calculate_agent_cost', false);
    }

    function handle_third_party_services(string $file_name, string $file_path, array $issues, array $company_agents, string $from_date, string $to_date)
    {
        $nextcloud_res = $open_project_res = $email_res = false;
        if ($this->third_party_integrations_nextcloud) {
            $nextcloud_res = $this->nextcloud_service->nextcloud_upload_csv($file_name, $file_path);
        }
        if ($this->third_party_integrations_open_project && $this->calculate_agent_cost) {
            $open_project_res = $this->open_project_service->open_project_add_task_to_agent($issues, $company_agents, $from_date,  $to_date);
        }
        if($this->email_send_allow) {
            $email_res = $this->email_service->send_export_via_email($from_date, $to_date, $file_name);
        }
        return [$nextcloud_res, $open_project_res, $email_res];
    }
}