<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Brand;
use Illuminate\Http\Request;
use Str;

class BrandController extends Controller
{
    public function store(Request $request)
    {
        $request->validate([
            'workspace_id' => 'required|exists:workspaces,id',
            'name' => 'required',
        ]);

        $brand = Brand::create([
            'workspace_id' => $request->workspace_id,
            'name' => $request->name,
            'slug' => Str::slug($request->name) . '-' . time(),
            'description' => $request->description,
        ]);

        return response()->json([
            'status' => true,
            'message' => 'Brand created successfully',
            'data' => $brand,
        ]);
    }

    public function index(Request $request)
    {
        $brands = Brand::where(
            'workspace_id',
            $request->workspace_id
        )->latest()->get();

        return response()->json([
            'status' => true,
            'data' => $brands,
        ]);
    }
}
