<?php

namespace App\Services\Opportunity;

use Exception;
use Carbon\Carbon;
use App\Enums\ActivityStatus;
use Illuminate\Support\Facades\DB;
use App\Http\Messages\ErrorMessages;
use App\Http\Messages\ExeptionMessages;
use App\Repositories\Activity\ActivityRepositoryInterface;
use App\Repositories\Activity\TypeActivityRepositoryInterface;
use App\Repositories\Opportunity\OpportunityRepositoryInterface;

class ActivityService
{
    protected $activityRepository;
    protected $typeactivityRepository;
    protected $opportunityRepository;
    public function __construct(
        ActivityRepositoryInterface $activityRepository,
        TypeActivityRepositoryInterface $typeactivityRepository,
        OpportunityRepositoryInterface $opportunityRepository

    ) {
        $this->activityRepository = $activityRepository;
        $this->typeactivityRepository = $typeactivityRepository;
        $this->opportunityRepository = $opportunityRepository;
    }

    public function getAllTypeActivity()
    {
        return $this->typeactivityRepository->all();
    }

    public function createActivity($data)
    {
        return DB::transaction(function () use ($data) {
            $data['scheduled_at'] = Carbon::parse($data['scheduled_at'])->format('Y-m-d H:i:s');


            $opportunity = $this->opportunityRepository->getOpportunityById($data['opportunity_id'])
            ?: throw new Exception(ExeptionMessages::OPPORTUNITY_NOT_FOUND, 404);

            $data['assigned_to'] = !isset($data['assigned_to']) || empty($data['assigned_to'])
            ? $opportunity->user_id
            : $data['assigned_to'];

            return $this->activityRepository->create($data);
        });
    }

    //TODO: una actividad no se actualiza
    public function updateActivity($id, $data)
    {
        return DB::transaction(function () use ($id, $data) {
            $data['scheduled_at'] = Carbon::parse($data['scheduled_at'])->format('Y-m-d H:i:s');
            return $this->activityRepository->update($id,$data);
        });
    }

    private function isActivityExist($id)
    {
        return $this->activityRepository->find($id)
            ?? throw new \Exception(ErrorMessages::ACTIVITY_NOT_FOUND, 404);
    }

    public function getActivityId($id)
    {
        return $this->isActivityExist($id);
    }

    public function getActivityByOpportunityAsesor($id_oportunity, $id_asesor)
    {
        return $this->activityRepository->getByOportunityAsesor($id_oportunity, $id_asesor);
    }

    public function getActivityByFechaAsesor($fecha, $id_asesor)
    {
        return $this->activityRepository->getByFechaAsesor($fecha, $id_asesor);
    }

    public function getPastActivitiesByFechaAsesor( $id_asesor)
    {
        return $this->activityRepository->getPastActivitiesAsesor($id_asesor);
    }
    public function getFutureActivitiesByFechaAsesor($id_asesor)
    {
        return $this->activityRepository->getFutureActivitiesAsesor($id_asesor);
    }

    public function getActivityByRangoFechaAsesor($fecha_inicio, $fecha_fin, $id_asesor)
    {
        if (!$fecha_inicio || !$fecha_fin) {
            throw new \Exception("Fecha de inicio y fin son requeridas",  400);
        }
        return $this->activityRepository->getByRangoFechasAsesor($fecha_inicio, $fecha_fin, $id_asesor);
    }

    public function changeStatus($id, $status)
    {
        if ($status == ActivityStatus::COMPLETED->value) {
            return $this->activityRepository->changeStatusCompleted($id, $status);
        }
        return $this->activityRepository->changeStatus($id, $status);
    }
}
