import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output, inject } from '@angular/core';
import { DecimalPipe } from '@angular/common';
import { RouterLink } from '@angular/router';
import { Product } from '../../../../../core/products/models/product-domain.model';
import { DialogComponent } from '../../../../../shared/ui/dialog/dialog.component';
import { ButtonComponent } from '../../../../../shared/ui/button/button.component';
import { TranslatePipe } from '../../../../../i18n/translate.pipe';
import { LangRoutePipe } from '../../../../../pipes/lang-route.pipe';
import { CurrencyConvertPipe } from '../../../../../pipes/currency-convert.pipe';
import { LanguageService } from '../../../../../services/language.service';
import { getDiscountedPrice, getMainImage } from '../../../../../utils/item.utils';

@Component({
  selector: 'app-quick-view-dialog',
  standalone: true,
  imports: [DialogComponent, ButtonComponent, TranslatePipe, LangRoutePipe, DecimalPipe, RouterLink, CurrencyConvertPipe],
  templateUrl: './quick-view-dialog.component.html',
  styleUrl: './quick-view-dialog.component.scss',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class QuickViewDialogComponent {
  private readonly languageService = inject(LanguageService);
  readonly displayCurrency = this.languageService.currentCurrency;

  @Input() product: Product | null = null;
  @Input() loading = false;

  @Output() closed = new EventEmitter<void>();
  @Output() addToCart = new EventEmitter<Product>();

  get mainImage(): string {
    return this.product ? getMainImage(this.product) : '';
  }

  get discountedPrice(): number {
    return this.product ? getDiscountedPrice(this.product) : 0;
  }

  get hasDiscount(): boolean {
    return !!this.product?.discount && this.product.discount > 0;
  }
}
