<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UpdatePasswordMobileController extends Controller
{
    public function updatePassword(Request $request)
    {
        $nisn = $request->input('nisn');
        $oldPassword = $request->input('old_password');
        $newPassword = $request->input('new_password');

        $user = DB::table('siswa')->where('nisn', $nisn)->first();

        if ($user && $user->password === $oldPassword) {
            // The old password matches, so update the password
            DB::table('siswa')->where('nisn', $nisn)->update([
                'password' => $newPassword,
            ]);

            $updatedUser = DB::table('siswa')->where('nisn', $nisn)->first();

            $response = array(
                'success' => true,
                'message' => 'Password telah diperbarui',
            );
            return response()->json($response);
        } else {
            return "Invalid old password";
        }
    }
}
