<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Models\DropDownBankList;
use Illuminate\Http\Request;

class DropDownBankController extends Controller
{
    public function index()
    {
        $lists = DropDownBankList::orderBy('name')->get();
        return response()->json($lists);
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'options' => 'required|array',
            'options.*' => 'string',
        ]);

        $list = DropDownBankList::create($validated);

        return response()->json($list, 201);
    }

    public function update(Request $request, $id)
    {
        $list = DropDownBankList::findOrFail($id);

        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'options' => 'required|array',
            'options.*' => 'string',
        ]);

        $list->update($validated);

        return response()->json($list);
    }

    public function destroy($id)
    {
        $list = DropDownBankList::findOrFail($id);
        $list->delete();

        return response()->json(['message' => 'Dropdown list deleted successfully']);
    }
}
