<?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 App\Services\ProofDownloadService;
use Illuminate\Http\Request;
// use Yajra\DataTables\Facades\DataTables;
use Illuminate\Support\Facades\Auth;

class CoordinatorController extends Controller
{
    // Coordinator dashboard (optional)
    public function index()
    {
        $user = Auth::user();
        $departments = Department::where('id', $user->department_id)->get();

        // Collect distinct years from all relevant date fields
        $years = collect([
            ActivitiesAttended::where('department_id', $user->department_id) ->selectRaw('YEAR(end_date) as year')->distinct()->pluck('year'),

            ActivitiesOrganised::where('department_id', $user->department_id) ->selectRaw('YEAR(end_date) as year')->distinct()->pluck('year'),

            BooksPublished::where('department_id', $user->department_id) ->selectRaw('YEAR(date_of_publication) as year')->distinct()->pluck('year'),

            ExternalEngagement::where('department_id', $user->department_id) ->selectRaw('YEAR(end_date) as year')->distinct()->pluck('year'),

            IvOrganised::where('department_id', $user->department_id) ->selectRaw('YEAR(end_date) as year')->distinct()->pluck('year'),

            OnlineCourse::where('department_id', $user->department_id) ->selectRaw('YEAR(end_date) as year')->distinct()->pluck('year'),

            Patent::where('department_id', $user->department_id) ->selectRaw('YEAR(date_of_submission) as year')->distinct()->pluck('year'),

            Publication::where('department_id', $user->department_id) ->selectRaw('YEAR(end_date) as year')->distinct()->pluck('year'),

        ])->flatten()->filter()->unique()->sortDesc()->values();

        return view('coordinator.dashboard', compact('departments', 'years'));
    }

    // View responses submitted by users
    public function viewActivitiesAttendedResponses()
    {
        $user = Auth::user();
        $departments = Department::where('id', $user->department_id)->get();
        return view('pages.activities-attended.index', compact('departments'));
    }

    public function viewActivitiesOrganisedResponses()
    {
        $user = Auth::user();
        $departments = Department::where('id', $user->department_id)->get();
        return view('pages.activities-organised.index', compact('departments'));
    }

    public function viewIvOrganisedResponses()
    {
        $user = Auth::user();
        $departments = Department::where('id', $user->department_id)->get();
        return view('pages.iv-organised.index', compact('departments'));
    }

    public function viewPublicationsResponses()
    {
        $user = Auth::user();
        $departments = Department::where('id', $user->department_id)->get();
        return view('pages.publications.index', compact('departments'));
    }

    public function viewBooksPublishedResponses()
    {
        $user = Auth::user();
        $departments = Department::where('id', $user->department_id)->get();
        return view('pages.booksPublished.index', compact('departments'));
    }

    public function viewExternalEngagementResponses()
    {
        $user = Auth::user();
        $departments = Department::where('id', $user->department_id)->get();
        return view('pages.externalEngagement.index', compact('departments'));
    }

    public function viewOnlineCoursesResponses()
    {
        $user = Auth::user();
        $departments = Department::where('id', $user->department_id)->get();
        return view('pages.onlineCourses.index', compact('departments'));
    }

    public function viewPatentsResponses()
    {
        $user = Auth::user();
        $departments = Department::where('id', $user->department_id)->get();
        return view('pages.patents.index', compact('departments'));
    }

    public function downloadProofs(Request $request, ProofDownloadService $proofDownloadService)
    {
        // Validate the request
        $request->validate([
            'ids' => 'required|string',
            'model' => 'sometimes|string',
        ]);

        $ids = json_decode($request->input('ids'));
        $modelName = $request->input('model', 'ActivitiesAttended');
        
        // Model mapping
        $modelMap = [
            'ActivitiesAttended' => ActivitiesAttended::class,
            'ActivitiesOrganised' => ActivitiesOrganised::class,
            'IvOrganised' => IvOrganised::class,
            'Publication' => Publication::class,
            'BooksPublished' => BooksPublished::class,
            'ExternalEngagement' => ExternalEngagement::class,
            'OnlineCourse' => OnlineCourse::class,
            'Patent' => Patent::class,
        ];
        
        // Get the model class from the map or default to ActivitiesAttended
        $modelClass = $modelMap[$modelName] ?? ActivitiesAttended::class;

        $result = $proofDownloadService->downloadProofs($modelClass, $ids);

        if (isset($result['error'])) {
            return back()->with('error', $result['error']);
        }

        // Return the zip file as a download
        return response()->download($result['filePath'], $result['fileName'])->deleteFileAfterSend(true);
    }

    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)
    {
        $user = Auth::user();
        $departmentId = $request->query('department_id');
        if (empty($departmentId) && $user && $user->role_id == 2) {
            $departmentId = $user->department_id;
        }
        $year = $request->query('year');

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

        $data = [];

        foreach ($models as $label => $modelConfig) {
            $query = $modelConfig['class']::query();
            if($departmentId) {
                $query->where('department_id', $departmentId);
            }
            if ($year && $year !== 'all') {
                $query->whereYear($modelConfig['date_field'], $year);
            }
            $count = $query->count();
            if ($count > 0) { // Only include data with non-zero 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)
    {
        $user = Auth::user();
        $model = $request->query('model', 'all');
        $year = $request->query('year');
        $departmentId = $request->query('department_id');
        if (empty($departmentId) && $user && $user->role_id == 2) {
            $departmentId = $user->department_id;
        }

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

        $data = [];

        if ($model === 'all') {
            $departments = Department::when($departmentId, function ($query) use ($departmentId) {
                return $query->where('id', $departmentId);
            })->get();

            foreach ($departments as $department) {
                $count = 0;
                foreach ($models as $modelConfig) {
                    $query = $modelConfig['class']::where('department_id', $department->id);
                    if ($year && $year !== 'all') {
                        $query->whereYear($modelConfig['date_field'], $year);
                    }
                    $count += $query->count();
                }
                if ($count > 0) { // Only include data with non-zero count
                    $data[] = [
                        'label' => $department->name,
                        'count' => $count,
                    ];
                }
            }
        } else {
            $modelConfig = $models[$model];
            $modelClass = $modelConfig['class'];
            $table = (new $modelClass)->getTable();

            $query = $modelClass::selectRaw('departments.name as label, COUNT(*) as count')
                ->join('departments', $table . '.department_id', '=', 'departments.id')
                ->groupBy('departments.name');

            if ($year && $year !== 'all') {
                $query->whereYear($table . '.' . $modelConfig['date_field'], $year);
            }

            if ($departmentId) {
                $query->where($table . '.department_id', $departmentId);
            }

            $data = $query->get()->filter(function ($item) {
                return $item['count'] > 0; // Filter out data with zero count
            })->toArray();
        }

        $total = array_sum(array_column($data, 'count'));

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