
    {h9i                     ,   d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	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 d d
lmZmZ d dlmZmZmZmZ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)  eddg      Z*e*jW                  ded       ee#       e       fdedefd       Z,e*jW                  ded       ee#      fdefd       Z-e*jW                  dd       ee#       e       fdedefd       Z.e*jW                  d d!       ee#       e
d"d#$      fded%e/fd&       Z0e*jc                  d'       ee#       e	d"       e       fded(edefd)       Z2e*jW                  d*ed+       ee#      fdefd,       Z-e*jW                  d-ed.       ee#      fd/e/defd0       Z3e*jW                  d1d2       ee#       e
d"d3$       e
d"d4$      fded5e/d6e/fd7       Z4e*jW                  d8d2       ee#       e
d"d3$      fded%e/fd9       Z5y):    N)Optional)UUID)	APIRouterDependsFileQuery
UploadFilestatus)StreamingResponse)text)Session)ResponseModel)ExportDataFilterSchema)get_all_latest_download_filesget_all_latest_uploaded_files)download_filedownload_file_by_namedownload_history_filedownload_matched_fileget_export_fileprocess_uploaded_file_datesave_uploaded_file)get_db)APIException)	constants)MAX_PER_PAGE zExport Data)prefixtagsz	/generatezDownload export file)response_modelsummarydbpayloadc                 r   K   t        | |       d{   }t        |ddt        j                        S 7 !w)a#  Download export file based on the provided filter criteria.
    Args:
        db (Session): Database session dependency.
        payload (ExportDataFilterSchema): Filter criteria for the export file.
    Returns:
        ResponseModel: Contains the export file data or an error message.
    r"   r#   NTz#Export file generated successfully.datasuccessmessagestatus_code)r   r   r
   HTTP_200_OK)r"   r#   r'   s      b/var/www/html/wine-match-dev/backend/winematch-backend/src/apps/export_data/routers/export_data.pydownload_export_filer-   "   s:      !B88D4)N\b\n\n  9   75"7z/latest-fileszGet latest export filesc                 p   K   t        |        d{   }t        |ddt        j                        S 7 !w)W  
    Get the latest export files based on the specified type and limit.
    Args:
        db (Session): Database session dependency.
    Returns:
        ResponseModel: Contains the latest export files or an error message.
    This endpoint retrieves the latest export files from the database.
    It uses the `get_export_latest_files` service to fetch the files.
    The response includes a success status, message, and the data containing the latest files.
    If no files are found, it returns an empty list.
    If an error occurs, it returns a failure response with an appropriate message.
    r"   NTz+Latest export files retrieved successfully.r&   )r   r   r
   r+   r"   r'   s     r,   get_latest_export_filesr3   4   s8       /"55D4)Vdjdvdv  6   64"6z	/downloadz'Download a specific export file by type)r!   c                 8   K   t        | |       d{   S 7 w)a8  
    Download a specific export file based on the provided type.
    Args:
        db (Session): Database session dependency.
        payload (ExportDataFilterSchema): Filter criteria for the export file, including type.
    Returns:
        ResponseModel: Contains the export file data or an error message.
    r%   N)r   r%   s     r,   download_specific_export_filer6   J   s      "g6666   z/download-by-namez'Download a specific export file by name.zName of the file to download)description	file_namec                 8   K   t        | |       d{   S 7 w)a  
    Download a specific export file based on the provided file name.
    Args:
        db (Session): Database session dependency.
        file_name (str): Name of the file to download.
    Returns:
        ResponseModel: Contains the export file data or an error message.
    r"   r9   N)r   r;   s     r,   download_export_file_by_namer<   Z         '"	BBBBr7   z/upload-csv/filec                    K   |j                   j                  d      st        ddt        j                  d      t        | ||       d{   }t        |ddt        j                  	      S 7 !w)
z0
    Upload a CSV file and save it locally.
    z.txtupload_filezOnly TXT files are allowed)moduleerrorr*   r)   )r"   r>   r#   NTz)Latest export files upoaded successfully.r&   )filenameendswithr   r
   HTTP_400_BAD_REQUESTr   r   r+   )r"   r>   r#   r'   s       r,   
upload_csvrF   j   sp      ==!!&) .330	
 	
 $rgFFD4)Tbhbtbt  Gs   A
A0A."A0z/latest-upliaded-fileszGet latest uploaded filesc                 p   K   t        |        d{   }t        |ddt        j                        S 7 !w)r0   r1   NTz-Latest uploaded files retrieved successfully.r&   )r   r   r
   r+   r2   s     r,   r3   r3      s8       /"55D4)Xflfxfx  6r4   z/process-uploaded-file/{type}zPreview uploaded CSV filetypec                 r   K   t        ||        d{   }t        |ddt        j                        S 7 !w)a  
    Process the uploaded CSV file based on the provided type.
    Args:
        db (Session): Database session dependency.
        type (str): Type of the uploaded file to process.
    Returns:
        ResponseModel: Contains the processed file data or an error message.
    )r"   rH   NTz%Uploaded file processed successfully.r&   )r   r   r
   r+   )rH   r"   r'   s      r,   process_uploaded_filerJ      s:      ,r==D4)P^d^p^p  >r.   z/get-matched-filez!Test endpoint to check API statusz+ID of the file to retrieve matched data forz-Date of the file to retrieve matched data forcodedatec                 :   K   t        | ||       d{   S 7 w)  
    Retrieve matched data for a specific file by its ID.
    Args:
        db (Session): Database session dependency.
        file_id (UUID, optional): ID of the file to retrieve matched data for.
    Returns:
        ResponseModel: Contains the matched data or an error message.
    r"   rK   rL   N)r   rO   s      r,   get_matched_filerP      s      '"4dCCCCs   z/get-history-filec                 8   K   t        | |       d{   S 7 w)rN   r;   N)r   r;   s     r,   get_history_filerR      r=   r7   )6csviotypingr   uuidr   fastapir   r   r   r   r	   r
   fastapi.responsesr   
sqlalchemyr   sqlalchemy.ormr   $src.apps.base.schemas.response_modelr   (src.apps.export_data.schemas.export_datar   +src.apps.export_data.services.download_filer   r   )src.apps.export_data.services.export_datar   r   r   r   r   r   r   src.core.dependenciesr   src.core.exceptionsr   	src.utilsr   src.utils.constantsr   routergetr-   r3   r6   strr<   postrF   rJ   rP   rR        r,   <module>ri      s   
 	   G G /  " > K   ) ,  ,	"M?	3 K?UV&/&-i# W" OMC\]&/ ^* K!JK&/&-i77#7 L7 )RS&/3,JKCCC TC ^&/Cy&-i
 $ . $]Lgh&/ i* +MSno &/
 p$ )LM&/c'TUc'VWDD
D D ND  )LM&/3,YZCCC NCrh   