<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AdminCourseController extends Controller
{
    public function index()
    {
        $courses = DB::table('courses')
            ->join('programmes', 'courses.programme_code', '=', 'programmes.programme_code')
            ->select(
                'courses.id',
                'courses.course_code',
                'courses.course_description',
                'courses.credits',
                'courses.level',
                'courses.semester',
                'courses.programme_code',
                'programmes.description as programme_description',
                'programmes.attendance_mode'
            )
            ->orderBy('programmes.description')
            ->orderBy('programmes.attendance_mode')
            ->orderBy('courses.level')
            ->orderBy('courses.semester')
            ->get();

        return view('admin.courses.index', compact('courses'));
    }

    public function create()
    {
        return view('admin.courses.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'course_code' => 'required|unique:courses,course_code',
            'course_description' => 'required',
            'credits' => 'required|numeric',
            'level' => 'required',
            'semester' => 'required',
            'programme_code' => 'required|exists:programmes,programme_code',
        ]);

        DB::table('courses')->insert([
            'course_code' => $request->course_code,
            'course_description' => $request->course_description,
            'credits' => $request->credits,
            'level' => $request->level,
            'semester' => $request->semester,
            'programme_code' => $request->programme_code,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        return redirect()->route('admin.courses.index')->with('success', 'Course added successfully.');
    }

    public function edit($id)
    {
        $course = DB::table('courses')->where('id', $id)->first();
        return view('admin.courses.edit', compact('course'));
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'course_code' => 'required|unique:courses,course_code,' . $id,
            'course_description' => 'required',
            'credits' => 'required|numeric',
            'level' => 'required',
            'semester' => 'required',
            'programme_code' => 'required|exists:programmes,programme_code',
        ]);

        DB::table('courses')->where('id', $id)->update([
            'course_code' => $request->course_code,
            'course_description' => $request->course_description,
            'credits' => $request->credits,
            'level' => $request->level,
            'semester' => $request->semester,
            'programme_code' => $request->programme_code,
            'updated_at' => now(),
        ]);

        return redirect()->route('admin.courses.index')->with('success', 'Course updated successfully.');
    }

    public function destroy($id)
    {
        DB::table('courses')->where('id', $id)->delete();
        return redirect()->route('admin.courses.index')->with('success', 'Course deleted successfully.');
    }
}
