o
    ?g6-                     @   s   d dl mZmZmZ d dlmZ d dlZd dlZd dlZd dl	m
Z
mZ d dlmZ d dlmZmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ edZG dd deZG dd dZdS )    )ListOptionalUnion)SessionN)HTTPException
UploadFile)Product)ProductResponseProductDetailResponseProductCreate)ProductImage)ProductCategory)	BaseModel)ProductCategoryBase)MediaService	adimsayarc                   @   s   e Zd ZU ee ed< dS )UpdateProductImagesRequestimagesN)__name__
__module____qualname__r   str__annotations__ r   r   $/app/app/services/product_service.pyr      s   
 r   c                   @   s  e Zd Zededee fddZededede	fddZ
edededee defd	d
Ze		ddededeee  dee def
ddZedededefddZedededefddZe		ddedededeee  dee defddZdS )ProductServicedbreturnc                 C   s2   |  t }tdt| d dd |D S )Nzget_products: found z	 productsc                 S   s   g | ]}t |qS r   )r	   from_orm).0productr   r   r   
<listcomp>   s    z/ProductService.get_products.<locals>.<listcomp>)queryr   allloggerinfolen)r   productsr   r   r   get_products   s   zProductService.get_products
product_idc                 C   s~   |  ttj|k }|stddd|j|j|j|j|j	r(dd |j	D ng |j
|jdk|j|j|jd
}tdi |S )	N  Product not foundstatus_codedetailc                 S   s   g | ]}|j qS r   )	image_url)r   imager   r   r   r!   &   s    z5ProductService.get_product_detail.<locals>.<listcomp>r   )
idnamedescriptionpricer   statusinStock
stockCount
created_at
updated_atr   )r"   r   filterr1   firstr   r2   r3   points_requiredr   r5   stockr8   r9   r
   )r   r)   r    r.   r   r   r   get_product_detail   s   z!ProductService.get_product_detail
image_urlsc                 C   sp   |  ttj|k }|stddd|j  t|D ]\}}t	|||d}|j
| q|   ddiS )Nr*   r+   r,   r)   r/   
sort_ordermessagez$Product images updated successfully.)r"   r   r:   r1   r;   r   r   clear	enumerater   appendcommit)r   r)   r?   r    idxurlnew_imgr   r   r   update_product_images/   s   
z$ProductService.update_product_imagesN
product_inr   media_servicec                 C   s  zt |j}| ttj|k }|stdddW n" ty>   | ttj	|jk }|s9tddd|j}Y nw |
 }||d< tdi |}| | |   | | |r>zt|dkrltdddtd|j d	|j	  |std
 td
td|j d|j  t|D ]x\}}	|	r|	jstd| d qtd|d  dt| d|	j  d|j	 d|j }
z%||	|
}td|  t|j||d}| | td|  W q ty } ztjd|	j dt| dd W Y d }~qd }~ww |   td|j  W |S  ty= } ztjdt| dd W Y d }~|S d }~ww |S )N  Invalid category IDr,   Invalid category codecategory_id   Maximum 5 images allowed.Starting image upload process for product ID: , category: *MediaService not provided for image upload"Using MediaService with endpoint: 
, bucket: Empty or invalid file at index 
, skippingProcessing image    / with filename: product/Successfully uploaded image to r@   )Added image record to database with URL: Failed to upload image : Texc_info/Completed image upload process for product ID: #Error during image upload process: r   )intrP   r"   r   r:   r1   r;   r   
ValueErrorcodedictr   addrF   refreshr&   r$   r%   errors3_endpoint_urlbucket_namerD   filenamewarningupload_imager   	Exceptionr   )r   rK   r   rL   rP   category_objproduct_datar    rG   upload_filefolder_pathr/   rI   img_errer   r   r   create_product@   sh   





&
,"zProductService.create_productcategory_inc                 C   s^   |  ttj|jk }|rtdddtdi | }| | |   | 	| |S )NrM   zCategory already existsr,   r   )
r"   r   r:   ri   r;   r   rj   rk   rF   rl   )r   r{   Zexisting_categoryZnew_categoryr   r   r   create_product_category   s   

z&ProductService.create_product_categoryc                 C   sb   |  ttj|k }|stddd|jD ]}| | q|   | | |   ddiS )Nr*   r+   r,   rB   zProduct deleted successfully.)	r"   r   r:   r1   r;   r   r   deleterF   )r   r)   r    r0   r   r   r   delete_product   s   

zProductService.delete_productc                 C   s  |  ttj|k }|stdddzt|j}|  ttj|k }|s0tdddW n" t	yS   |  ttj
|jk }|sNtddd|j}Y nw |jdd}||d	< | D ]
\}	}
t||	|
 qb|rNzt|d
kr}tdddtd|j d|j
  |std t	dtd|j d|j  |j  t|D ]x\}}|r|jstd| d qtd|d  dt| d|j  d|j
 d|j }z%|||}td|  t|j||d}| | td|  W q ty# } ztjd|j dt| dd W Y d }~qd }~ww td|j  W n tyM } ztjdt| dd W Y d }~nd }~ww |   | | |S ) Nr*   r+   r,   rM   rN   rO   T)exclude_unsetrP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r@   r`   ra   rb   rc   re   rf   ) r"   r   r:   r1   r;   r   rg   rP   r   rh   ri   rj   itemssetattrr&   r$   r%   rm   rn   ro   r   rC   rD   rp   rq   rr   r   rk   rs   r   rF   rl   )r   r)   rK   r   rL   r    rP   rt   ru   keyvaluerG   rv   rw   r/   rI   rx   ry   r   r   r   update_product   sj   	




&
,$
zProductService.update_product)NN)r   r   r   staticmethodr   r   r	   r(   rg   r
   r>   r   rj   rJ   r   r   r   r   r   rz   r   r   r|   r~   r   r   r   r   r   r      sR     
H
r   ) typingr   r   r   sqlalchemy.ormr   loggingosuuidfastapir   r   app.models.productr   app.schemas.productr	   r
   r   Zapp.models.product_imager   app.models.product_categoryr   pydanticr   Zapp.schemas.product_categoryr   app.services.media_servicer   	getLoggerr$   r   r   r   r   r   r   <module>   s     
