<?php

declare(strict_types=1);

namespace Kylin\Education\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Kylin\Education\Models\Course;

/**
 * 课程控制器
 */
class CourseController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = Course::with(['teacher', 'category']);

        if ($request->filled('status')) {
            $query->where('status', $request->input('status'));
        }
        if ($request->filled('type')) {
            $query->where('type', $request->input('type'));
        }
        if ($request->filled('keyword')) {
            $query->where('name', 'like', "%{$request->input('keyword')}%");
        }

        $courses = $query->paginate($request->input('per_page', 15));

        return response()->json(['code' => 0, 'data' => $courses]);
    }

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:200',
            'code' => 'nullable|string|max:50|unique:courses',
            'category_id' => 'nullable|exists:course_categories,id',
            'type' => 'required|in:online,offline,hybrid',
            'hours' => 'required|integer|min:1',
            'duration' => 'required|integer|min:1',
            'max_students' => 'required|integer|min:1',
            'price' => 'required|numeric|min:0',
            'age_range' => 'nullable|string|max:50',
            'description' => 'nullable|string',
            'cover_image' => 'nullable|string|max:500',
            'teacher_id' => 'nullable|exists:users,id',
        ]);

        $course = Course::create($validated);

        return response()->json(['code' => 0, 'data' => $course], 201);
    }

    public function show(Course $course): JsonResponse
    {
        $course->load(['teacher', 'category', 'chapters']);

        return response()->json(['code' => 0, 'data' => $course]);
    }

    public function update(Request $request, Course $course): JsonResponse
    {
        $validated = $request->validate([
            'name' => 'sometimes|string|max:200',
            'code' => 'nullable|string|max:50|unique:courses,code,' . $course->id,
            'category_id' => 'nullable|exists:course_categories,id',
            'type' => 'sometimes|in:online,offline,hybrid',
            'hours' => 'sometimes|integer|min:1',
            'duration' => 'sometimes|integer|min:1',
            'max_students' => 'sometimes|integer|min:1',
            'price' => 'sometimes|numeric|min:0',
            'age_range' => 'nullable|string|max:50',
            'description' => 'nullable|string',
            'cover_image' => 'nullable|string|max:500',
            'teacher_id' => 'nullable|exists:users,id',
        ]);

        $course->update($validated);

        return response()->json(['code' => 0, 'data' => $course]);
    }

    public function destroy(Course $course): JsonResponse
    {
        $course->delete();

        return response()->json(['code' => 0, 'message' => '删除成功']);
    }

    public function submitReview(Course $course): JsonResponse
    {
        $course->update(['status' => 'pending_review']);

        return response()->json(['code' => 0, 'message' => '已提交审核']);
    }

    public function publish(Course $course): JsonResponse
    {
        $course->update(['status' => 'published']);

        return response()->json(['code' => 0, 'message' => '课程已发布']);
    }

    public function archive(Course $course): JsonResponse
    {
        $course->update(['status' => 'archived']);

        return response()->json(['code' => 0, 'message' => '课程已归档']);
    }
}
