<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\PenempatanBankService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class BankController extends Controller
{
    protected $penempatanService;

    public function __construct(PenempatanBankService $penempatanService)
    {
        $this->penempatanService = $penempatanService;
    }

    public function index()
    {
        return $this->penempatanService->getAllBank();
    }

    public function store(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'nama' => 'required|string|unique:table_bank,nama',
                'alamat' => 'required|string',
                'nomor_telp' => 'string',
            ]);

            $role = $this->penempatanService->createBank($validator->validated());
            return response()->json($role, 201);
        } catch (\Exception $e) {
            return response()->json(['message' => $e->getMessage()], 400);
        }
    }

    public function show(string $id)
    {
        return $this->penempatanService->findById($id);
    }

    public function update(Request $request, string $id)
    {
        try {
            $validator = Validator::make($request->all(), [
                'id' => 'required',
                'nama' => 'required|string',
                'alamat' => 'required|string',
                'nomor_telp' => 'string',
            ]);

            $role = $this->penempatanService->updateBank($id, $validator->validated());
            return response()->json($role, 201);
        } catch (\Exception $e) {
            return response()->json(['message' => $e->getMessage()], 400);
        }
    }

    public function destroy(string $id)
    {
        return $this->penempatanService->deleteBank($id);
    }
}
