<?php
namespace App\Models;

use App\Services\CurrencyService;

class Product extends Model {
    protected static function getTable(): string {
        return 'products';
    }

    public function getCategories() {
        $cats = $this->categories;
        if (is_string($cats)) {
            $cats = json_decode($cats, true) ?? [];
        }
        if (empty($cats)) {
            return [];
        }
        return Category::all(['slug' => $cats]);
    }

    public function getConvertedPrice($targetCurrency = null) {
        $targetCurrency = $targetCurrency ?: CurrencyService::getCurrentCurrency();
        $sourceCurrency = $this->currency ?: 'TRY';
        $price = (float)($this->price ?: 0.0);
        
        return CurrencyService::convert($price, $sourceCurrency, $targetCurrency);
    }

    public function getFormattedPrice($targetCurrency = null) {
        $targetCurrency = $targetCurrency ?: CurrencyService::getCurrentCurrency();
        $converted = $this->getConvertedPrice($targetCurrency);
        return CurrencyService::format($converted, $targetCurrency);
    }

    public function getOriginalPriceFormatted() {
        return CurrencyService::format($this->price, $this->currency);
    }

    public function getImages(): array {
        $images = [];
        if (!empty($this->images)) {
            $decoded = is_string($this->images) ? json_decode($this->images, true) : $this->images;
            if (is_array($decoded)) {
                $images = $decoded;
            }
        }
        
        // Fallback to legacy single image if no images array exists
        if (empty($images) && !empty($this->image) && is_string($this->image)) {
            $images[] = $this->image;
        }
        
        // Normalize all image paths
        $normalizedImages = [];
        foreach ($images as $img) {
            // If it's a URL, keep it
            if (strpos($img, 'http') === 0) {
                $normalizedImages[] = $img;
                continue;
            }
            
            // If it's a new upload, it starts with /uploads/
            if (strpos($img, '/uploads/') === 0) {
                $normalizedImages[] = $img;
                continue;
            }
            
            // If it's already got /image/, keep it
            if (strpos($img, '/image/') === 0) {
                $normalizedImages[] = $img;
                continue;
            }
            
            // Otherwise it's a legacy path without /image/ prefix
            if (strpos($img, '/') === 0) {
                $normalizedImages[] = '/image' . $img;
            } else {
                $normalizedImages[] = '/image/' . $img;
            }
        }
        
        return $normalizedImages;
    }

    public function getMainImage($baseUrl = '') {
        $imgs = $this->getImages();
        if (empty($imgs)) {
            $imageBase = \App\Models\Settings::get('image_base_url', $baseUrl . '/image');
            return $imageBase . '/cache/placeholder-200x200.png';
        }
        
        $img = $imgs[0];
        if (strpos($img, 'http') === 0) {
            return $img;
        }
        
        return $baseUrl . $img;
    }
}
