<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\PaintingCategory;
use App\Repositories\Interfaces\Productinterface;
use App\Traits\FileUploadTrait;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ProductController extends Controller
{

    use FileUploadTrait;
    private $repoProduct;

    public function __construct(Productinterface $repoProduct/*,OrderInterface $repoOrder*/)
    {
        $this->repoProduct = $repoProduct;
    }

    public function list(Request $request)
    {
        $products   = $this->repoProduct->list($request);
        $categories = PaintingCategory::where('is_delete', 0)->where('status', 1)->get();

        return view('Admin.Product.list')
            ->with('records', $products)
            ->with('categories', $categories);
    }

    public function create(Request $request)
    {
        $Category = PaintingCategory::where('is_delete', 0)->where('status', 1)->orderBy('name_en', 'ASC')->get();
        return view('Admin.Product.add')
            ->with('Categories', $Category);
    }

    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'name_en'          => 'required|string',
            'cat_id'           => 'required|exists:product_category,id',
            'composition'      => 'nullable|string',
            'introduction'     => 'nullable|string',
            'benefits'         => 'nullable|string',
            'details'          => 'nullable|string',
            'meta_description' => 'nullable|string',
            'meta_title'       => 'nullable|string',
            'meta_keywords'    => 'nullable|string',
            'meta_author'      => 'nullable|string',
            'brand_logo'       => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'img_1'            => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'img_2'            => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'img_3'            => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'img_4'            => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
        ]);

                                     // Define category IDs (adjust these based on your FileCategory setup)
        $brandLogoCategoryId    = 1; // Category ID for brand logos
        $productImageCategoryId = 2; // Category ID for product images

        try {
            // Upload brand logo if present
            $brand_logo = $request->hasFile('brand_logo')
            ? $this->uploadFile($brandLogoCategoryId, $request->file('brand_logo'))
            : null;

            // Upload product images if present
            $img_1 = $request->hasFile('img_1')
            ? $this->uploadFile($productImageCategoryId, $request->file('img_1'))
            : null;
            $img_2 = $request->hasFile('img_2')
            ? $this->uploadFile($productImageCategoryId, $request->file('img_2'))
            : null;
            $img_3 = $request->hasFile('img_3')
            ? $this->uploadFile($productImageCategoryId, $request->file('img_3'))
            : null;
            $img_4 = $request->hasFile('img_4')
            ? $this->uploadFile($productImageCategoryId, $request->file('img_4'))
            : null;

            // Store product using repository
            $product = $this->repoProduct->store(
                $request,
                $brand_logo,
                $img_1,
                $img_2,
                $img_3,
                $img_4
            );

            return redirect()->route('product.list')->with('success', 'Product created successfully!');

        } catch (\Exception $e) {
            return back()->withInput()->with('error', 'Error uploading files: ' . $e->getMessage());
        }
    }

    public function edit(Request $request, $id)
    {
        $product = $this->repoProduct->find($id);
        if (! $product) {
            abort(404, 'Product not found');
        }

        $categories = PaintingCategory::where('is_delete', 0)->where('status', 1)->orderBy('name_en', 'ASC')->get();

        return view('Admin.Product.edit', [
            'product'    => $product,
            'Categories' => $categories,
        ]);
    }


     public function productMeta(Request $request, $id)
    {
        $product = $this->repoProduct->find($id);
        if (! $product) {
            abort(404, 'Product not found');
        }

        $categories = PaintingCategory::where('is_delete', 0)->where('status', 1)->orderBy('name_en', 'ASC')->get();

        return view('Admin.Product.meta', [
            'product'    => $product,
            'Categories' => $categories,
        ]);
    }

    public function update(Request $request, $id)
    {
        // Validate the request data
        $validatedData = $request->validate([
            'name_en'          => 'required|string',
            'cat_id'           => 'required|exists:product_category,id',
            'composition'      => 'nullable|string',
            'introduction'     => 'nullable|string',
            'benefits'         => 'nullable|string',
            'details'          => 'nullable|string',
            'meta_description' => 'nullable|string',
            'meta_title'       => 'nullable|string',
            'meta_keywords'    => 'nullable|string',
            'meta_author'      => 'nullable|string',
            'brand_logo'       => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'img_1'            => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'img_2'            => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'img_3'            => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
            'img_4'            => 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
        ]);

        // Find the product
        $product = $this->repoProduct->find($id);
        if (! $product) {
            return redirect()->route('product.list')->with('error', 'Product not found!');
        }

                                     // Define category IDs (should match what you used in store)
        $brandLogoCategoryId    = 1; // Category ID for brand logos
        $productImageCategoryId = 2; // Category ID for product images

        // Prepare update data
        $updateData = [
            'name_en'          => $request->name_en,
            'cat_id'           => $request->cat_id,
            'composition'      => $request->composition,
            'introduction'     => $request->introduction,
            'benefits'         => $request->benefits,
            'details'          => $request->details,
            'meta_description' => $request->meta_description,
            'meta_title'       => $request->meta_title,
            'meta_keywords'    => $request->meta_keywords,
            'meta_author'      => $request->meta_author,
        ];

        // Handle file uploads if provided
        if ($request->hasFile('brand_logo')) {
            $updateData['brand_logo'] = $this->uploadFile($brandLogoCategoryId, $request->file('brand_logo'));
        }

        if ($request->hasFile('img_1')) {
            $updateData['img_1'] = $this->uploadFile($productImageCategoryId, $request->file('img_1'));
        }

        if ($request->hasFile('img_2')) {
            $updateData['img_2'] = $this->uploadFile($productImageCategoryId, $request->file('img_2'));
        }

        if ($request->hasFile('img_3')) {
            $updateData['img_3'] = $this->uploadFile($productImageCategoryId, $request->file('img_3'));
        }

        if ($request->hasFile('img_4')) {
            $updateData['img_4'] = $this->uploadFile($productImageCategoryId, $request->file('img_4'));
        }

        // Update the product
        $this->repoProduct->update($id, $updateData);

        return redirect()->route('product.list')->with('success', 'Product updated successfully!');
    }


     public function update_meta(Request $request, $id)
    {
        // Validate the request data
        $validatedData = $request->validate([
            'meta_description' => 'nullable|string',
            'meta_title'       => 'nullable|string',
            'meta_keywords'    => 'nullable|string',
            'meta_author'      => 'nullable|string',
        ]);

        // Find the product
        $product = $this->repoProduct->find($id);
        if (! $product) {
            return redirect()->route('product.list')->with('error', 'Product not found!');
        }

                                     // Define category IDs (should match what you used in store)
        $brandLogoCategoryId    = 1; // Category ID for brand logos
        $productImageCategoryId = 2; // Category ID for product images

        // Prepare update data
        $updateData = [

            'meta_description' => $request->meta_description,
            'meta_title'       => $request->meta_title,
            'meta_keywords'    => $request->meta_keywords,
            'meta_author'      => $request->meta_author,
        ];
        // Handle file uploads if provided
        // Update the product
        $this->repoProduct->update($id, $updateData);
        return redirect()->route('product.list')->with('success', 'Product updated successfully!');
    }

    public function delete($id)
    {
        $deletedCity = $this->repoProduct->delete($id);

        if ($deletedCity) {
            // Handle successful soft delete
            return redirect()->route('product.list')->with('success', 'Product deleted successfully');
        } else {
            // Handle case where doctor is not found
            return redirect()->route('product.list')->with('error', 'Product not found');
        }
    }

}
