<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;

class ProfileController extends Controller
{
    /**
     * Tampilkan profil lengkap santri & user
     */
    public function santriProfile(Request $request)
    {
        $user = $request->user();
        $santri = $user->santri;

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

        return response()->json([
            'user' => [
                'id' => $user->id,
                'name' => $user->name,
                'username' => $user->username,
                'email' => $user->email,
                'role' => $user->role,
            ],
            'santri' => [
                'nis' => $santri->nis,
                'alamat' => $santri->alamat,
                'tempat_lahir' => $santri->tempat_lahir,
                'tanggal_lahir' => $santri->tanggal_lahir,
                'jenis_kelamin' => $santri->jenis_kelamin,
                'foto_url' => $santri->foto ? asset('storage/' . $santri->foto) : null,
            ],
        ]);
    }

    /**
     * Update profil user & santri
     */
    public function updateSantriProfile(Request $request)
    {
        $user = $request->user();
        $santri = $user->santri;

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

        $validator = Validator::make($request->all(), [
            'name' => 'sometimes|string|max:255', // user.name
            'alamat' => 'sometimes|string|max:255',
            'tempat_lahir' => 'sometimes|string|max:100',
            'tanggal_lahir' => 'sometimes|date',
            'jenis_kelamin' => 'sometimes|in:L,P',
            'foto' => 'sometimes|image|mimes:jpeg,png,jpg|max:2048',
        ]);

        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 422);
        }

        // Update user.name
        if ($request->has('name')) {
            $user->name = $request->name;
            $user->save();
        }

        // Update data santri
        $santri->alamat = $request->get('alamat', $santri->alamat);
        $santri->tempat_lahir = $request->get('tempat_lahir', $santri->tempat_lahir);
        $santri->tanggal_lahir = $request->get('tanggal_lahir', $santri->tanggal_lahir);
        $santri->jenis_kelamin = $request->get('jenis_kelamin', $santri->jenis_kelamin);

        if ($request->hasFile('foto')) {
            // Hapus foto lama jika ada
            if ($santri->foto && Storage::disk('public')->exists($santri->foto)) {
                Storage::disk('public')->delete($santri->foto);
            }

            $santri->foto = $request->file('foto')->store('foto_profile', 'public');
        }

        $santri->save();

        return response()->json([
            'message' => 'Profil berhasil diperbarui',
            'user' => $user,
            'santri' => $santri,
            'foto_url' => $santri->foto ? asset('storage/' . $santri->foto) : null,
        ]);

    }
    public function changePassword(Request $request)
    {
        $request->validate([
            'old_password' => 'required',
            'new_password' => 'required|min:6',
            'confirm_password' => 'same:new_password'
        ]);

        $user = auth()->user();

        if (!Hash::check($request->old_password, $user->password)) {
            return response()->json(['message' => 'Password lama salah'], 422);
        }

        $user->update(['password' => bcrypt($request->new_password)]);
        return response()->json(['message' => 'Password berhasil diperbarui']);
    }
    public function updatePassword(Request $request)
    {
        $request->validate([
            'old_password' => 'required|string',
            'new_password' => 'required|string|min:6',
        ]);

        if (!Hash::check($request->old_password, $request->user()->password)) {
            return response()->json(['message' => 'Password lama salah'], 403);
        }

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

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