o
    X^g                     @   s  d dl mZmZmZmZmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZmZmZ d dlmZ e Zejdeejd	eejeejfd
edefddZejdedeejeejfdedefddZejde	e dd deejeejfdededefddZejdedeejeejfdedefddZejdededddeejfd e
e defd!d"Z dS )#    )	APIRouterDependsHTTPExceptionstatusQuery)Session)ListOptional)datetime)deps)OrderCreateOrderResponseOrderSummary)order_servicez	/checkout)response_modelstatus_code
order_datadbc                 C   s6   d}| j r
| j  }tj||j|| j| j| jd}|S )aH  
    Creates a new order from the user's shopping cart.
    
    This endpoint:
    1. Validates product availability
    2. Checks if user has enough points
    3. Creates an order record
    4. Creates order item records
    5. Updates product stock
    6. Deducts points from user's balance
    7. Clears the user's shopping cart
    
    You can provide shipping details directly OR use an address_id to reference a saved address.
    If an address_id is provided, it will be used instead of shipping_details.
    
    Additionally, you can save the shipping details as a new address by setting save_address to True
    and providing an address_title. This is only applicable when providing shipping_details directly.
    
    If any validation fails (e.g., insufficient stock, insufficient points),
    the entire operation fails.
    N)r   user_idshipping_details
address_idsave_addressaddress_title)r   dictr   create_order_from_cartidr   r   r   )r   r   current_userr   Z	new_order r   "/app/app/api/v1/endpoints/order.pyr      s   
	r   z/{order_id})r   order_idc                 C   s&   t || |j}|sttjdd|S )zo
    Retrieves a specific order by ID.
    Only returns the order if it belongs to the authenticated user.
    zOrder not foundr   detail)r   Zget_order_by_idr   r   r   HTTP_404_NOT_FOUND)r   r   r   orderr   r   r   	get_order;   s   
r$   /d   skiplimitc                 C   s   t ||j| |}|S )z
    Retrieves all orders for the authenticated user.
    Results are paginated and sorted by creation date (newest first).
    )r   get_user_ordersr   )r'   r(   r   r   ordersr   r   r   r)   N   s   r)   z/{order_id}/cancelc                 C   s   t j|| |jd}|S )aq  
    Cancels an existing order.
    
    This endpoint:
    1. Validates that the order exists and belongs to the user
    2. Checks if the order is in a cancellable state
    3. Changes the order status to "CANCELLED"
    4. Restores product stock
    5. Refunds the points used for the order
    
    Only orders in PENDING or PROCESSING status can be cancelled.
    )r   r   r   )r   cancel_orderr   )r   r   r   Zcancelled_orderr   r   r   r+   ]   s   r+   z/summary/weeklyNuN   기준 날짜 (YYYY-MM-DD 형식). 제공되지 않으면 오늘 날짜 기준)descriptiondatec                 C   sB   d}| rzt | d}W n ty   ttjddw t||S )u   
    주간 주문 요약 정보를 제공합니다.
    - 주간 고유 사용자 수
    - 주간 총 주문 건수
    - 주간 총 포인트 사용량
    
    로그인 없이 접근 가능합니다.
    Nz%Y-%m-%dz2Invalid date format. Please use YYYY-MM-DD format.r    )r
   strptime
ValueErrorr   r   HTTP_400_BAD_REQUESTr   get_weekly_order_summary)r-   r   Ztarget_dater   r   r   r1   x   s   r1   )!fastapir   r   r   r   r   sqlalchemy.ormr   typingr   r	   r
   Zapp.apir   Zapp.schemas.orderr   r   r   app.servicesr   routerpostHTTP_201_CREATEDget_dbget_current_userr   getintr$   r)   r+   strr1   r   r   r   r   <module>   sh    -
