<?php

namespace App\Services;

use App\Models\Product;
use App\Models\User;

class PriceService
{
    /**
     * Get the appropriate price for a product based on customer type
     *
     * @param Product $product
     * @param User|null $customer
     * @return float
     */
    public function getPriceForCustomer(Product $product, ?User $customer = null): float
    {
        // If no customer, return sale price
        if (is_null($customer)) {
            return (float) $product->price;
        }

        // Get customer type (default to 'retail' if null or empty)
        $customerType = $customer->customer_type ?? 'retail';
        if (empty($customerType)) {
            $customerType = 'retail';
        }

        // If customer is wholesale, return wholesale_price or fallback to sale price
        if ($customerType === 'wholesale') {
            return (float) ($product->wholesale_price ?? $product->price);
        }

        // Default: retail customer - return sale price
        return (float) $product->price;
    }

    /**
     * Get the price label based on customer type
     *
     * @param User|null $customer
     * @return string
     */
    public function getPriceLabel(?User $customer = null): string
    {
        if (is_null($customer) || $customer->customer_type === 'retail' || empty($customer->customer_type)) {
            return 'Sale Price';
        }

        if ($customer->customer_type === 'wholesale') {
            return 'Wholesale Price';
        }

        return 'Sale Price';
    }
}

