<?php

namespace App\Repositories\Activity;

use App\Enums\ActivityStatus;
use App\Models\Activity;
use App\Repositories\Activity\ActivityRepositoryInterface;
use Carbon\Carbon;

class ActivityRepository implements ActivityRepositoryInterface
{
    public function create(array $data)
    {
        return Activity::create($data);
    }
    public function find($id)
    {
        return Activity::find($id);
    }
    public function update($id, $data)
    {
        return Activity::find($id)->update($data);
    }
    public function delete($id)
    {
        return Activity::find($id)->delete();
    }
    public function all()
    {
        return Activity::all();
    }
    public function allquery()
    {
        return Activity::query();
    }
    public function restore($id)
    {
        return Activity::withTrashed()->find($id)->restore();
    }
    public function getByOportunityAsesor($id_oportunity, $id_asesor)
    {
        return Activity::where('opportunity_id', $id_oportunity)
            ->where('assigned_to', $id_asesor)
            ->get();
    }
    public function getByRangoFechasAsesor($fecha_inicio, $fecha_fin, $id_asesor)
    {
        return Activity::whereBetween('scheduled_at', [$fecha_inicio, $fecha_fin])  // Rango de fechas
            ->where('assigned_to', $id_asesor)
            ->where('state_activity', ActivityStatus::PENDING)
            ->get();
    }
    public function getByFechaAsesor($fecha, $id_asesor)
    {
        $currentTime = now();
        return Activity::whereDate('scheduled_at', $fecha)
            ->whereTime('scheduled_at', '>', $currentTime->format('H:i:s')) // Solo actividades futuras en el mismo día
            ->where('assigned_to', $id_asesor)
            ->where('state_activity', ActivityStatus::PENDING)
            ->orderBy('scheduled_at', 'asc')
            ->get();
    }

    public function getPastActivitiesAsesor($id_asesor)
    {
        $currentTime = now();
        return Activity::where('scheduled_at', '<', $currentTime)
            ->where('assigned_to', $id_asesor)
            ->where('state_activity', ActivityStatus::PENDING)
            ->orderBy('scheduled_at', 'desc')
            ->get();
    }

    public function getFutureActivitiesAsesor($id_asesor)
    {
        $tomorrow = now()->addDay()->startOfDay();
        return Activity::where('scheduled_at', '>=', $tomorrow)
            ->where('assigned_to', $id_asesor)
            ->where('state_activity', ActivityStatus::PENDING)
            ->orderBy('scheduled_at', 'asc')
            ->get();
    }

    public function changeStatus($id, $status)
    {
        $activity = Activity::find($id);
        $activity->state_activity  = $status;
        $activity->save();
        return $activity;
    }

    public function changeStatusCompleted($id)
    {
        $activity = Activity::find($id);
        $activity->state_activity  = ActivityStatus::COMPLETED;
        $activity->completed_at =  Carbon::now();
        $activity->save();
        return $activity;
    }
}
