o
    Eg                     @   s  d dl mZmZmZmZmZmZ d dlmZm	Z	m
Z
 d dlmZ d dl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 e Zejd	ee d
eejfdefddZejded
eejfdedefddZejd	ed
edeg eejeejfde dee dedefddZ!ej"ded
edeg eejeejfdede dee dedef
ddZ#ej$de%d
eejfdedefddZ&dS )    )	APIRouterDepends
UploadFileFileFormHTTPException)ListUnionOptional)SessionN)deps)ProductResponseProductDetailResponseProductCreate)ProductService)MediaService/)response_modeldbc                 C   s
   t | S )zRetrieve the list of products)r   Zget_products)r    r   %/app/app/api/v1/endpoints/products.pylist_products   s   
r   z/{product_id}
product_idc                 C      t || S )z1Retrieve product details for the given product ID)r   get_product_detailr   r   r   r   r   r         r   .
product_inimagesmedia_servicec                    sT   zt | }W n ty   tdddw tdi |}t||||}t|S )a&  
    Create a new product by providing a JSON string containing the product data via 'product_in'.
    
    Expected form-data:
    - product_in: JSON string with the following keys:
        {
            "name": "Product Name",
            "description": "Product description",
            "category_id": 1,
            "points_required": 100,
            "stock": 50,
            "status": 1,
            "specifications": {"color": "blue", "size": "M"}  // optional
        }
    - images: (Optional) file upload(s) for product images (max 5)
      $Invalid JSON provided for product_instatus_codedetailNr   )	jsonloads	Exceptionr   r   r   Zcreate_productr   from_orm)r   r   r   r   product_dataproduct_payloadproductr   r   r   create_product_endpoint   s   
r,   c                    sV   zt |}W n ty   tdddw tdi |}t|| |||}t|S )a;  
    Update an existing product by providing a JSON string containing the product data via 'product_in'.
    
    Expected form-data:
    - product_in: JSON string with the following keys:
        {
            "name": "Updated Product Name",
            "description": "Updated product description",
            "category_id": 1,
            "points_required": 100,
            "stock": 50,
            "status": 1,
            "specifications": {"color": "red", "size": "L"}  // optional
        }
    - images: (Optional) file upload(s) for product images (max 5)
    r    r!   r"   Nr   )	r%   r&   r'   r   r   r   Zupdate_productr   r(   )r   r   r   r   r   r)   r*   r+   r   r   r   update_product_endpoint7   s   
r-   c                 C   r   )zDelete a product by its ID)r   Zdelete_productr   r   r   r   delete_product_endpointX   r   r.   )'fastapir   r   r   r   r   r   typingr   r	   r
   sqlalchemy.ormr   r%   Zapp.apir   Zapp.schemas.productr   r   r   Zapp.services.product_servicer   app.services.media_servicer   routergetget_dbr   intr   postget_media_servicestrr,   putr-   deletedictr.   r   r   r   r   <module>   sZ      "