<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use App\Services\ImageService;

class InvestmentProductImage extends Model
{
    protected $fillable = [
        'investment_product_id',
        'path',
        'filename',
        'thumbnail_path',
        'position',
        'is_primary',
        'alt_text',
        'size',
        'ai_analysis'
    ];

    protected $casts = [
        'is_primary' => 'boolean',
        'position' => 'integer',
        'size' => 'integer',
        'ai_analysis' => 'array'
    ];

    /**
     * Relation avec le produit d'investissement
     */
    public function investmentProduct()
    {
        return $this->belongsTo(InvestmentProduct::class);
    }

    /**
     * Obtenir l'URL complète de l'image
     */
    public function getUrlAttribute(): string
    {
        return ImageService::getUrl($this->path, 'images/no-investment.svg');
    }

    /**
     * Obtenir l'URL de la miniature
     */
    public function getThumbnailUrlAttribute(): string
    {
        if ($this->thumbnail_path) {
            return ImageService::getUrl($this->thumbnail_path, 'images/no-investment.svg');
        }
        return ImageService::getThumbnailUrl($this->path, 'images/no-investment.svg');
    }

    /**
     * Définir cette image comme principale
     */
    public function setAsPrimary(): void
    {
        // Retirer le flag des autres images
        self::where('investment_product_id', $this->investment_product_id)
            ->where('id', '!=', $this->id)
            ->update(['is_primary' => false]);

        // Mettre à jour cette image
        $this->update(['is_primary' => true]);

        // Mettre à jour l'image principale du produit
        $this->investmentProduct->update([
            'image' => $this->path,
            'thumbnail' => $this->thumbnail_path
        ]);
    }

    /**
     * Supprimer le fichier physique
     */
    public function deleteFile(): void
    {
        // Supprimer l'image principale
        if ($this->path) {
            if (Storage::disk('public')->exists($this->path)) {
                Storage::disk('public')->delete($this->path);
            }
            $publicPath = public_path('storage/' . $this->path);
            if (file_exists($publicPath)) {
                @unlink($publicPath);
            }
        }

        // Supprimer la miniature
        if ($this->thumbnail_path) {
            if (Storage::disk('public')->exists($this->thumbnail_path)) {
                Storage::disk('public')->delete($this->thumbnail_path);
            }
            $publicThumb = public_path('storage/' . $this->thumbnail_path);
            if (file_exists($publicThumb)) {
                @unlink($publicThumb);
            }
        }
    }

    /**
     * Scope pour les images ordonnées
     */
    public function scopeOrdered($query)
    {
        return $query->orderBy('position');
    }

    /**
     * Scope pour l'image principale
     */
    public function scopePrimary($query)
    {
        return $query->where('is_primary', true);
    }
}
