<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreBucketRequest;
use App\Http\Resources\BucketResource;
use App\Models\Board;
use App\Models\Bucket;
use Illuminate\Http\Request;

class BucketController extends Controller
{
    public function index() {
//        $board_id = Board::where('code', $board_code)->first()->id;

        return BucketResource::collection(Bucket::get());
    }

    public function store(StoreBucketRequest $request) {
        $fields = $request->validated();

        $bucket = Bucket::create($fields);

        return response()->json([
            "bucket" => new BucketResource($bucket)
        ], 201);
    }

    public function updateList(Request $request) {
        $buckets = $request->buckets;

        foreach ($buckets as $bucket) {
            Bucket::where('id', (int)$bucket['id'])->update([
                'order' => $bucket['order']
            ]);
        }

        return response()->json([
            "message" => "Buckets updated successfully"
        ], 200);
    }
}
