<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Santri;
use Illuminate\Support\Facades\Hash;

class ForgotPasswordController extends Controller
{
    public function validateNIS(Request $request)
    {
        $request->validate([
            'nis' => 'required|exists:santris,nis'
        ]);

        return response()->json(['message' => 'NIS valid']);
    }

    public function resetPassword(Request $request)
    {
        $request->validate([
            'nis' => 'required|exists:santris,nis',
            'new_password' => 'required|min:6',
            'confirm_password' => 'required|same:new_password'
        ]);

        $santri = Santri::where('nis', $request->nis)->first();

        if (!$santri || !$santri->user) {
            return response()->json(['message' => 'User tidak ditemukan'], 404);
        }

        $santri->user->update([
            'password' => Hash::make($request->new_password)
        ]);

        return response()->json(['message' => 'Password berhasil direset']);
    }
}
