<?php

namespace Modules\Shop\Http\Controllers\Admin;

use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Shop\Entities\Product; // <--- این خط حتما باید اضافه شود

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index()
    {
        return view('shop::index');
    }

    /**
     * Show the form for creating a new resource.
     * @return Renderable
     */
    public function create()
    {
        return view('shop::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param Request $request
     * @return Renderable
     */
    public function store(Request $request)
    {
        // ساخت یک محصول تستی در دیتابیس
        $product = Product::create([
            'title' => $request->title,
            'slug' => \Illuminate\Support\Str::slug($request->title) . '-' . rand(100,999),
            'sku' => 'PRD-' . rand(1000, 9999),
            'price' => $request->price,
            'stock_quantity' => 10,
            'is_active' => true,
            // ذخیره داده‌ها به صورت آرایه (لاراول خودش این را به JSON تبدیل می‌کند)
            'attributes' => [
                'color' => $request->color,
                'weight' => '1.5kg',
                'material' => 'Cotton'
            ],
            'meta_data' => [
                'seo_title' => 'خرید ' . $request->title,
                'keywords' => ['test', 'product', 'ajax']
            ]
        ]);

        return response()->json([
            'success' => true,
            'message' => 'محصول با موفقیت ثبت شد!',
            'data' => $product
        ]);
    }

    /**
     * Show the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function show($id)
    {
        return view('shop::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function edit($id)
    {
        return view('shop::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Renderable
     */
    public function destroy($id)
    {
        //
    }
}