<?php

namespace App\Http\Controllers;

use App\Models\CourseEnrollment;
use App\Repositories\Contracts\CourseEnrollmentRepositoryInterface;
use App\Http\Traits\ResponseTrait;
use App\Http\Resources\ResponseCollection;
use Illuminate\Http\Request;

class CourseEnrollmentController extends Controller
{
    use ResponseTrait;

    private $courseEnrollmentRepository;

    public function __construct(CourseEnrollmentRepositoryInterface $courseEnrollmentRepository)
    {
        $this->courseEnrollmentRepository = $courseEnrollmentRepository;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $perPage = $request->per_page ? $request->per_page : 100;

        return new ResponseCollection(
            $this->paginateResponse($this->courseEnrollmentRepository->all($request), $perPage)
        );
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // $request->validate([
        //     'name' => 'required|string|max:255',
        //     'location' => 'required|string|max:255',
        //     'owner_id' => 'required|numeric',
        // ]);

        $courseEnrollment = $this->courseEnrollmentRepository->create($request->toArray());

        return $this->successResponse($courseEnrollment, 'Course enrollment added successfully!');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\CourseEnrollment  $courseEnrollment
     * @return \Illuminate\Http\Response
     */
    public function show(CourseEnrollment $courseEnrollment)
    {
        return $this->successResponse($courseEnrollment);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\CourseEnrollment  $courseEnrollment
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, CourseEnrollment $courseEnrollment)
    {
        $this->courseEnrollmentRepository->update($courseEnrollment, $request->toArray());

        return $this->updateResponse('Course Enrollment updated successfully!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\CourseEnrollment  $courseEnrollment
     * @return \Illuminate\Http\Response
     */
    public function destroy(CourseEnrollment $courseEnrollment)
    {
        $this->courseEnrollmentRepository->delete($courseEnrollment);

        return $this->updateResponse('Course Enrollment deleted successfully!');
    }
}
