<?php

namespace App\Http\Controllers;

use App\Models\StudentWaiver;
use App\Models\Student;
use App\Models\FeeType;
use Illuminate\Http\Request;

class StudentWaiverController extends Controller
{
    public function index() {
        $waivers = StudentWaiver::with(['student', 'feeType'])->latest()->get();
        $students = Student::where('status', 'Active')->get();
        $feeTypes = FeeType::all();
        return view('admin.waivers.index', compact('waivers', 'students', 'feeTypes'));
    }

    public function store(Request $request) {
        $request->validate([
            'student_id' => 'required',
            'fee_type_id' => 'required',
            'discount_value' => 'required|numeric'
        ]);

        StudentWaiver::create($request->all());
        return response()->json(['message' => 'ওয়েভার সফলভাবে যুক্ত হয়েছে!']);
    }

    public function edit($id)
	{
		$waiver = StudentWaiver::with(['student', 'feeType'])->find($id);
		return response()->json($waiver);
	}

    public function update(Request $request) {
        $waiver = StudentWaiver::findOrFail($request->waiver_id);
        $waiver->update($request->all());
        return response()->json(['message' => 'ওয়েভার আপডেট করা হয়েছে!']);
    }

    public function destroy($id) {
        StudentWaiver::findOrFail($id)->delete();
        return response()->json(['message' => 'ওয়েভার মুছে ফেলা হয়েছে!']);
    }
}