<?php

namespace App\Http\Controllers;

use App\Models\StudentProfile;
use App\Repositories\Contracts\StudentProfileRepositoryInterface;
use App\Http\Traits\ResponseTrait;
use App\Http\Resources\ResponseCollection;
use Illuminate\Http\Request;

class StudentProfileController extends Controller
{
    use ResponseTrait;

    private $studentProfileRepository;

    public function __construct(StudentProfileRepositoryInterface $studentProfileRepository)
    {
        $this->studentProfileRepository = $studentProfileRepository;
    }

    /**
     * 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->studentProfileRepository->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',
        // ]);

        $studentProfile = $this->studentProfileRepository->create($request->toArray());

        return $this->successResponse($studentProfile, 'Student profile added successfully!');
    }

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

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

        return $this->updateResponse('Student Profile updated successfully!');
    }

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

        return $this->updateResponse('Student Profile deleted successfully!');
    }
}
