<?php

namespace App\Http\Controllers\Api\Student;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Announcement;
use Illuminate\Support\Facades\Auth;
use App\Models\StudentClass;
use App\Models\StudentRegister;
use App\Models\StudyGuide;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\Validator;

class AnnouncementController extends Controller
{
    // Display All Student Announcements
    public function index()
    {
        try {

            $data = [];
            // Get the authenticated student's ID
            $studentId = Auth::guard('sanctum')->user()->id;

            // Fetch student registration details
            $studentRegister = StudentRegister::where('student_id', $studentId)->first();

            // Check if the student register exists
            if (!$studentRegister) {
                // If student is not registered, return empty data
                return response()->json([
                    'data' => ['stats' => $data],
                    'response_status' => 200,
                ], 200);
            }

            // Fetch all class/section records for the student
            $studentClasses = StudentClass::where('student_register_id', $studentRegister->id)->get();
    
            if ($studentClasses->isEmpty()) {
                return response()->json([
                    'validation_errors' => ['error' => ['No class or section found for this student.']],
                    'response_status' => 400,
                ], 200);
            }
    
            // Collect announcements for each class-section
            $allAnnouncements = collect();
    
            foreach ($studentClasses as $studentClass) {
                $class = $studentClass->class_id;
                $section = $studentClass->section;
    
                $announcements = Announcement::select('id','title','description','subject_id')
                    ->where('end_date', '>=', Carbon::now()->toDateString())
                    ->where('class_id', $class)
                    ->where('section', $section)
                    ->with('subject:id,subject_name')
                    ->latest()
                    ->get();
    
                $allAnnouncements = $allAnnouncements->merge($announcements);
            }
    
            // Optional: remove duplicate announcements if same announcement is assigned to multiple sections
            $uniqueAnnouncements = $allAnnouncements->unique('id')->values();
    
            return response()->json([
                'data' => ['announcements' => $uniqueAnnouncements],
                'response_status' => 200,
            ], 200);
        } catch (Exception $exception) {
            // Handle any errors that occur
            return response()->json([
                'validation_errors' => ['error' => [$exception->getMessage()]],
                'response_status' => 400,
            ], 200);
        }
    }
    
    public function getStudyGuide()
    {
        try {
            $studentId = Auth::guard('sanctum')->user()->id;
    
            $studentRegister = StudentRegister::where('student_id', $studentId)->first();
    
            if (!$studentRegister) {
                return response()->json([
                    'data' => ['studyGuides' => []],
                    'response_status' => 200,
                ]);
            }
    
            // Get all classes associated with the student
            $studentClasses = StudentClass::where('student_register_id', $studentRegister->id)->get();
    
            if ($studentClasses->isEmpty()) {
                return response()->json([
                    'data' => ['studyGuides' => []],
                    'response_status' => 200,
                ]);
            }
    
            $studyGuides = collect();
    
            foreach ($studentClasses as $studentClass) {
                $guides = StudyGuide::select('id', 'study_guide_name', 'file','year','class_id','section','subject_id')->with('classRelation:id,class_name','subject:id,subject_name')
                    ->where('class_id', $studentClass->class_id)
                    ->where('section', $studentClass->section)
                    ->latest()
                    ->get();
    
                $studyGuides = $studyGuides->merge($guides);
            }
    
            return response()->json([
                'data' => ['studyGuides' => $studyGuides->values()],
                'response_status' => 200,
            ],200);
        } catch (Exception $exception) {
            return response()->json([
                'validation_errors' => ['error' => [$exception->getMessage()]],
                'response_status' => 400,
            ],200);
        }
    }

}
