<?php

namespace App\Http\Controllers;

use App\Models\Rekening;
use Illuminate\Http\Request;

class RekeningController extends Controller
{
    public function create(Request $request)
    {
        try {
            $validatedData = $request->validate([
                'bank_name' => 'required|string',
                'atas_nama' => 'required|string',
                'norek' => 'required|integer',
            ]);

            $validatedData['status'] = 'on';

            $rekening = Rekening::create($validatedData);

            return response()->json($rekening, 201);
        } catch (\Illuminate\Database\QueryException $e) {
            return response()->json(['error' => 'Failed to create rekening'], 500);
        } catch (\Exception $e) {
            return response()->json(['error' => 'Internal Server Error'], 500);
        }
    }
    public function getAll()
    {
        try {
            $rekenings = Rekening::all();
            return response()->json([
                'status' => 'success',
                'data' => $rekenings
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to retrieve Rekening: ' . $e->getMessage()
            ], 500);
        }
    }

    public function getActive()
    {
        try {
            $rekenings = Rekening::where('status', 'on')->get();

            return response()->json($rekenings, 200);
        } catch (\Exception $e) {
            return response()->json(['error' => 'Internal Server Error'], 500);
        }
    }

    public function update(Request $request, $id)
    {
        try {
            $rekening = Rekening::findOrFail($id);
            $validatedData = $request->validate([
                'bank_name' => 'required|string',
                'atas_nama' => 'required|string',
                'norek' => 'required|integer',
                'status' => 'required|string',
            ]);

            $rekening->update($validatedData);

            return response()->json($rekening, 200);
        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            return response()->json(['error' => 'Rekening not found'], 404);
        } catch (\Illuminate\Database\QueryException | \Exception $e) {
            return response()->json(['error' => 'Failed to create'], 500);
        }
    }

    public function delete(string $id)
    {
        try {
            $rekening = Rekening::findOrFail($id);
            $rekening->delete();

            return response()->json(['message' => 'Rekening deleted successfully'], 200);
        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            return response()->json(['error' => 'Rekening not found'], 404);
        } catch (\Illuminate\Database\QueryException | \Exception $e) {
            return response()->json(['error' => 'Failed to delete rekening'], 500);
        }
    }

    public function restore($id)
    {
        try {
            $propertyType = Rekening::withTrashed()->findOrFail($id);
            $propertyType->restore();

            return response()->json([
                'status' => 'success',
                'message' => 'Rekening type restored successfully',
                'data' => $propertyType
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to restore property type: ' . $e->getMessage()
            ], 500);
        }
    }

    public function trashed()
    {
        try {
            $trashedFasility = Rekening::onlyTrashed()->get();
            
            if ($trashedFasility->isEmpty()) {
                return response()->json([
                    'success' => true,
                    'message' => 'Tidak ada data role yang dihapus.',
                    'data' => []
                ], 200);
            }

            return response()->json([
                'success' => true,
                'message' => 'Data role yang dihapus berhasil diambil.',
                'data' => $trashedFasility
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Gagal mengambil data role yang dihapus.',
                'error' => $e->getMessage()
            ], 500);
        }
    }
    /**
     * Permanently delete the specified resource.
     */
    public function forceDestroy($id)
    {
        try {
            $propertyType = Rekening::withTrashed()->findOrFail($id);
            $propertyType->forceDelete();

            return response()->json([
                'status' => 'success',
                'message' => 'Rekening type permanently deleted successfully'
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to permanently delete Rekening: ' . $e->getMessage()
            ], 500);
        }
    }
}
