<?php

namespace App\Repositories\Dashboard;

use App\Models\Activity;
use Carbon\Carbon;

class DashboardSellerRepository implements DashboardSellerRepositoryInterface
{
    public function countActivity($fromDate, $toDate, $asesor_id)
    {
        return Activity::where('assigned_to', $asesor_id)
            ->whereBetween('scheduled_at', [$fromDate, $toDate])
            ->count();
    }
    public function countActivityProject($fromDate, $toDate, $asesor_id, $projectId)
    {
        return Activity::where(function ($query) use ($projectId) {
            $query->whereNull('opportunity_id')
                ->orWhereHas('opportunity', function ($query) use ($projectId) {
                    $query->whereHas('property', function ($query) use ($projectId) {
                        $query->where('project_id', $projectId);
                    });
                });
        })
            ->where('assigned_to', $asesor_id)
            ->whereBetween('scheduled_at', [$fromDate, $toDate])
            ->count();
    }

    public function countActivityType($fromDate, $toDate, $asesor_id, $type_id)
    {
        // dd($fromDate, $toDate, $asesor_id, $type_id);
        return Activity::where('assigned_to', $asesor_id)
            ->where('type_activity_id', $type_id)
            ->whereBetween('scheduled_at', [$fromDate, $toDate])
            ->count();
    }
    public function countActivityTypeProject($fromDate, $toDate, $asesor_id, $type_id, $projectId)
    {
        return Activity::where(function ($query) use ($projectId) {
            $query->whereNull('opportunity_id')
                ->orWhereHas('opportunity', function ($query) use ($projectId) {
                    $query->whereHas('property', function ($query) use ($projectId) {
                        $query->where('project_id', $projectId);
                    });
                });
        })
            ->where('assigned_to', $asesor_id)
            ->where('type_activity_id', $type_id)
            ->whereBetween('scheduled_at', [$fromDate, $toDate])
            ->count();
    }

    public function countActivityStage($fromDate, $toDate, $asesor_id, $stage)
    {
        return Activity::where('assigned_to', $asesor_id)
            ->where('state_activity', $stage)
            ->whereBetween('scheduled_at', [$fromDate, $toDate])
            ->count();
    }
    public function countActivityStageProject($fromDate, $toDate, $asesor_id, $stage, $projectId)
    {
        return Activity::where(function ($query) use ($projectId) {
            $query->whereNull('opportunity_id')
                ->orWhereHas('opportunity', function ($query) use ($projectId) {
                    $query->whereHas('property', function ($query) use ($projectId) {
                        $query->where('project_id', $projectId);
                    });
                });
        })
            ->where('assigned_to', $asesor_id)
            ->where('state_activity', $stage)
            ->whereBetween('scheduled_at', [$fromDate, $toDate])
            ->count();
    }

    public function TimeAverageActivityStage($fromDate, $toDate, $asesor_id, $stage)
    {
        $totalTime =  Activity::where('assigned_to', $asesor_id)
            ->where('state_activity', $stage)
            ->whereNotNull('completed_at')
            ->whereBetween('scheduled_at', [$fromDate, $toDate])
            ->get()
            ->reduce(function ($carry, $activity) {
                $createdAt = Carbon::parse($activity->created_at);
                $completedAt = Carbon::parse($activity->completed_at);
                $differenceInHours = $createdAt->diffInDays($completedAt);
                return $carry + $differenceInHours;
            }, 0);

        $activityCount = Activity::where('assigned_to', $asesor_id)
            ->where('state_activity', $stage)
            ->whereNotNull('completed_at')
            ->whereBetween('scheduled_at', [$fromDate, $toDate])
            ->count();

        if ($activityCount === 0) {
            return 0;
        }
        $averageTime = $totalTime / $activityCount;
        return round($averageTime, 2);
    }

    public function TimeAverageActivityStageProject($fromDate, $toDate, $asesor_id, $stage, $projectId)
    {
        $totalTime =  Activity::where(function ($query) use ($projectId) {
            $query->whereNull('opportunity_id')
                ->orWhereHas('opportunity', function ($query) use ($projectId) {
                    $query->whereHas('property', function ($query) use ($projectId) {
                        $query->where('project_id', $projectId);
                    });
                });
        })
            ->where('assigned_to', $asesor_id)
            ->whereNotNull('completed_at')
            ->where('state_activity', $stage)
            ->whereBetween('scheduled_at', [$fromDate, $toDate])
            ->get()
            ->reduce(function ($carry, $activity) {
                $createdAt = Carbon::parse($activity->created_at);
                $completedAt = Carbon::parse($activity->completed_at);
                $differenceInHours = $createdAt->diffInDays($completedAt);
                return $carry + $differenceInHours;
            }, 0);

        $activityCount = Activity::where(function ($query) use ($projectId) {
            $query->whereNull('opportunity_id')
                ->orWhereHas('opportunity', function ($query) use ($projectId) {
                    $query->whereHas('property', function ($query) use ($projectId) {
                        $query->where('project_id', $projectId);
                    });
                });
        })
            ->where('assigned_to', $asesor_id)
            ->whereNotNull('completed_at')
            ->where('state_activity', $stage)
            ->whereBetween('scheduled_at', [$fromDate, $toDate])
            ->count();

        if ($activityCount === 0) {
            return 0;
        }
        $averageTime = $totalTime / $activityCount;
        return round($averageTime, 2);
    }
}
