<?php

namespace App\Http\Controllers;

use App\Models\ActivitiesAttended;
use App\Models\ActivitiesOrganised;
use App\Models\BooksPublished;
use App\Models\Department;
use App\Models\ExternalEngagement;
use App\Models\IvOrganised;
use App\Models\OnlineCourse;
use App\Models\Patent;
use App\Models\Publication;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    // Admin dashboard (optional)
    public function index()
    {
        $departments = Department::all();
        return view('admin.dashboard', compact('departments'));
    }

    // View responses submitted by users
    public function viewActivitiesAttendedResponses()
    {
        $departments = Department::all();
        return view('pages.activities-attended.index', compact('departments'));
    }
    public function viewActivitiesOrganisedResponses()
    {
        return view('pages.activities-organised.index');
    }
    public function viewIvOrganisedResponses()
    {
        return view('pages.iv-organised.index');
    }
    public function viewPublicationsResponses()
    {
        return view('pages.publications.index');
    }
    public function viewBooksPublishedResponses()
    {
        return view('pages.booksPublished.index');
    }
    public function viewExternalEngagementResponses()
    {
        return view('pages.externalEngagement.index');
    }
    public function viewOnlineCoursesResponses()
    {
        return view('pages.onlineCourses.index');
    }
    public function viewPatentsResponses()
    {
        return view('pages.patents.index');
    }
    public function analyticsActivitiesAttended()
    {
        $data = ActivitiesAttended::selectRaw('departments.name as department, COUNT(*) as count')
            ->join('departments', 'activities_attendeds.department_id', '=', 'departments.id')
            ->groupBy('departments.name')
            ->get();

        $total = $data->sum('count');

        return response()->json([
            'labels' => $data->pluck('department'),
            'values' => $data->pluck('count'),
            'total' => $total,
        ]);
    }

    public function analyticsActivitiesOrganised()
    {
        $data = ActivitiesOrganised::selectRaw('departments.name as department, COUNT(*) as count')
            ->join('departments', 'activities_organiseds.department_id', '=', 'departments.id')
            ->groupBy('departments.name')
            ->get();

        $total = $data->sum('count');

        return response()->json([
            'labels' => $data->pluck('department'),
            'values' => $data->pluck('count'),
            'total' => $total,
        ]);
    }

    public function analyticsIvOrganised()
    {
        $data = IvOrganised::selectRaw('departments.name as department, COUNT(*) as count')
            ->join('departments', 'iv_organiseds.department_id', '=', 'departments.id')
            ->groupBy('departments.name')
            ->get();

        $total = $data->sum('count');

        return response()->json([
            'labels' => $data->pluck('department'),
            'values' => $data->pluck('count'),
            'total' => $total,
        ]);
    }
    
    public function analyticsPaperPublished()
    {
        $data = Publication::selectRaw('departments.name as department, COUNT(*) as count')
            ->join('departments', 'publications.department_id', '=', 'departments.id')
            ->groupBy('departments.name')
            ->get();

        $total = $data->sum('count');

        return response()->json([
            'labels' => $data->pluck('department'),
            'values' => $data->pluck('count'),
            'total' => $total,
        ]);
    }

    public function analyticsBooksPublished()
    {
        $data = BooksPublished::selectRaw('departments.name as department, COUNT(*) as count')
            ->join('departments', 'books_published.department_id', '=', 'departments.id')
            ->groupBy('departments.name')
            ->get();

        $total = $data->sum('count');

        return response()->json([
            'labels' => $data->pluck('department'),
            'values' => $data->pluck('count'),
            'total' => $total,
        ]);
    }

    public function analyticsExternalEngagement()
    {
        $data = ExternalEngagement::selectRaw('departments.name as department, COUNT(*) as count')
            ->join('departments', 'external_engagements.department_id', '=', 'departments.id')
            ->groupBy('departments.name')
            ->get();

        $total = $data->sum('count');

        return response()->json([
            'labels' => $data->pluck('department'),
            'values' => $data->pluck('count'),
            'total' => $total,
        ]);
    }

    public function analyticsOnlineCourse()
    {
        $data = OnlineCourse::selectRaw('departments.name as department, COUNT(*) as count')
            ->join('departments', 'online_courses.department_id', '=', 'departments.id')
            ->groupBy('departments.name')
            ->get();

        $total = $data->sum('count');

        return response()->json([
            'labels' => $data->pluck('department'),
            'values' => $data->pluck('count'),
            'total' => $total,
        ]);
    }
    public function analyticsPatent()
    {
        $data = Patent::selectRaw('departments.name as department, COUNT(*) as count')
            ->join('departments', 'patents.department_id', '=', 'departments.id')
            ->groupBy('departments.name')
            ->get();

        $total = $data->sum('count');

        return response()->json([
            'labels' => $data->pluck('department'),
            'values' => $data->pluck('count'),
            'total' => $total,
        ]);
    }

    public function analyticsComparison(Request $request)
    {
        $departmentId = $request->query('department_id');

        $models = [
            'ActivitiesAttended' => ActivitiesAttended::class,
            'ActivitiesOrganised' => ActivitiesOrganised::class,
            'BooksPublished' => BooksPublished::class,
            'ExternalEngagement' => ExternalEngagement::class,
            'IvOrganised' => IvOrganised::class,
            'OnlineCourse' => OnlineCourse::class,
            'Patent' => Patent::class,
            'Publication' => Publication::class,
        ];

        $data = [];

        foreach ($models as $label => $model) {
            $count = $model::where('department_id', $departmentId)->count();
            $data[] = [
                'label' => $label,
                'count' => $count,
            ];
            $total = array_sum(array_column($data, 'count'));

        }

        return response()->json([
            'labels' => array_column($data, 'label'),
            'values' => array_column($data, 'count'),
            'total' => $total
        ]);
    }

    public function analyticsContribution(Request $request)
    {
        $model = $request->query('model', 'all');

        $models = [
            'ActivitiesAttended' => ActivitiesAttended::class,
            'ActivitiesOrganised' => ActivitiesOrganised::class,
            'BooksPublished' => BooksPublished::class,
            'ExternalEngagement' => ExternalEngagement::class,
            'IvOrganised' => IvOrganised::class,
            'OnlineCourse' => OnlineCourse::class,
            'Patent' => Patent::class,
            'Publication' => Publication::class,
        ];

        $data = [];

        if ($model === 'all') {
            $departments = Department::all();

            foreach ($departments as $department) {
                $count = 0;
                foreach ($models as $modelClass) {
                    $count += $modelClass::where('department_id', $department->id)->count();
                }
                $data[] = [
                    'label' => $department->name,
                    'count' => $count,
                ];
                $total = array_sum(array_column($data, 'count'));
            }
        } else {
            $modelClass = $models[$model];
            $data = $modelClass::selectRaw('departments.name as label, COUNT(*) as count')
                ->join('departments', 'department_id', '=', 'departments.id')
                ->groupBy('departments.name')
                ->get()
                ->toArray();
            $total = array_sum(array_column($data, 'count'));
        }

        return response()->json([
            'labels' => array_column($data, 'label'),
            'values' => array_column($data, 'count'),
            'total' => $total
        ]);
    }
}
