<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\Product;
use App\Services\ProductService;

class ProductController extends Controller
{
    private $productService;

    public function __construct(ProductService $productService)
    {
        $this->productService = $productService;
    }

    public function show($slug, ProductService $productService)
    {
        $product = Product::with([
            'plans',
            'plans.pricingTiers',
            'plans.pricingTiers.addons',
            'plans.pricingTiers.addons.plans',
            'reviews',
            'category',
            'supportPolicy',
            'faqs'
        ])->where('slug', $slug)->firstOrFail();

        // Get structured pricing data from service
        $pricingData = $productService->preparePricingData($product);
        
        // Extract components for clean theme access
        $productType = $pricingData['product_type'];
        $defaultPlan = $pricingData['default_plan'];
        $billingTiers = $pricingData['billing_tiers'];
        $hasMultiplePlans = $pricingData['has_multiple_plans'];

        $title = $product->getMeta('seo_title') ?? $product->name;
        $description = $product->getMeta('seo_description') ?? null;

        // Rating Logic
        $reviews = $product->reviews;
        $totalReviews = $reviews->count();
        $averageRating = $totalReviews ? round($reviews->avg('rating'), 1) : 0;
        
        // Star Breakdown for Progress Bars
        $starCounts = $reviews->groupBy('rating')->map->count();
        $percentages = [];
        for ($i = 5; $i >= 1; $i--) {
            $count = $starCounts[$i] ?? 0;
            $percentages[$i] = $totalReviews ? round(($count / $totalReviews) * 100) : 0;
        }

        // Header Stars Logic
        $fullStars = floor($averageRating);
        $halfStar = $averageRating - $fullStars >= 0.5;
        $emptyStars = 5 - ($fullStars + ($halfStar ? 1 : 0));
        
        // Breadcrumbs
        $breadcrumbs = [
            ['label' => __('general.Home'), 'url' => route('home')],
            ['label' => __('general.Shop'), 'url' => route('frontend.products.all')],
        ];
        if ($product->category) {
            $breadcrumbs[] = [
                'label' => $product->category->name,
                'url' => route('frontend.products.category', $product->category->slug)
            ];
        }
        $breadcrumbs[] = ['label' => $product->name, 'url' => null];
        
        $pageHeader = ['breadcrumbs' => $breadcrumbs];

        return view('theme::pages.shop.show', compact(
            'product',
            'title',
            'description',
            'pricingData',
            'productType',
            'defaultPlan',
            'billingTiers',
            'hasMultiplePlans',
            'averageRating',
            'totalReviews',
            'percentages',
            'fullStars',
            'halfStar',
            'emptyStars',
            'reviews',
            'pageHeader'
        ));
    }
}
