U
    TDi%                     @   s   d Z ddlZddlmZmZmZmZ ddlmZ ddlm	Z	m
Z
mZmZ G dd dejZG dd	 d	e	ZG d
d dejZG dd de	ZdS )zScrapy items for product data.    N)OptionalListDictAny)datetime)	BaseModelHttpUrlFieldfield_validatorc                   @   s   e Zd ZdZe Ze Ze Ze Z	e Z
e Ze Ze Ze Ze Ze Ze Ze Ze Ze Ze Ze ZdS )ProductItemz/Scrapy item for product data (raw dict format).N)__name__
__module____qualname____doc__scrapyr	   namepriceoriginal_pricecurrencydescriptionimagesproduct_urlskucategory
attributesavailabilitystock_statusratingreview_countreviews
source_url
crawled_at r"   r"   scrapy_project/items.pyr   	   s$   r   c                   @   s6  e Zd ZU dZeddddZeed< eddd	d
Ze	e
 ed< edddd
Ze	e
 ed< edddZeed< edddZe	e ed< eeddZee ed< edddZe	e ed< edddZe	e ed< edddZe	e ed< edddZe	eeef  ed< edd dZe	e ed!< edd"dZe	e ed#< eddd$d%d&Ze	e
 ed'< eddd(d
Ze	e ed)< eed*dZee ed+< edd,dZe	e ed-< eejd.dZeed/< e de!ee ee d0d1d2Z"e de!eed0d3d4Z#e!e$d d5d6d7Z%e&d8d9d:Z'e&d8d;d<Z(ee& d8d=d>Z)dS )?Productz*Pydantic model for validated product data..   zProduct name)Z
min_lengthr   r   Nr   zProduct price)ger   r   zOriginal price if on saler   ZVNDzCurrency code)defaultr   r   zProduct description)r   r   zList of image URLs)default_factoryr   r   zProduct page URLr   zProduct SKU/IDr   zProduct categoryr   z&Product attributes (size, color, etc.)r   zAvailability statusr   z%Stock status (in_stock, out_of_stock)r      zProduct rating (0-5))r&   ler   r   zNumber of reviewsr   zxList of product reviews. Each review is a dict with: author, rating, date, content, images (optional list of image URLs)r   zSource website URLr    zCrawl timestampr!   )vreturnc                 C   s   dd |D S )zEnsure image URLs are valid.c                 S   s    g | ]}|r|  r|  qS r"   strip).0Zimgr"   r"   r#   
<listcomp>@   s       z+Product.validate_images.<locals>.<listcomp>r"   clsr+   r"   r"   r#   validate_images<   s    zProduct.validate_imagesc                 C   s   |r|  S dS )zClean product name. r-   r1   r"   r"   r#   validate_nameB   s    zProduct.validate_name)itemr,   c                 C   sV   t |}t|dtr4|d r*|d gng |d< nt|dtsLg |d< | f |S )z Create Product from Scrapy item.r   )dict
isinstancegetstrlist)r2   r6   datar"   r"   r#   from_scrapy_itemH   s    zProduct.from_scrapy_item)r,   c              '   C   s   d| j p
d| jddd| jpddd | jp,ddddd| jd	krBdnddddddddddd| jrj| jrj| jnd| jptd| jp|ddd| jrd
| jnddddddddddd&S )z"Convert to WooCommerce CSV format.Zsimpler4   r%   r   ZvisibleN   ZtaxableZin_stock|)&TypeZSKUName	PublishedzIs featured?zVisibility in catalogzShort descriptionZDescriptionzDate sale price startszDate sale price endsz
Tax statusz	Tax classz	In stock?ZStockzLow stock amountzBackorders allowed?zSold individually?zWeight (kg)zLength (cm)z
Width (cm)zHeight (cm)zAllow customer reviews?zPurchase notez
Sale pricezRegular priceZ
CategoriesTagszShipping classZImageszDownload limitzDownload expiry daysZParentzGrouped productsZUpsellszCross-sellszExternal URLzButton textZPosition)	r   r   r   r   r   r   r   r   joinselfr"   r"   r#   to_woocommerce_csv_rowT   sN    zProduct.to_woocommerce_csv_rowc                 C   s   |   r|   d S i S )z@Convert to Shopify CSV format (single row, backward compatible).r   )to_shopify_csv_rowsrE   r"   r"   r#   to_shopify_csv_row   s    zProduct.to_shopify_csv_rowc              0   C   s2  | j p| j dddd }|| jdd | jp6dd| jp@ddddddddd| j pXddd	dd
d| jpjd| jprdddd| jd| j| jr| jdd nddddddddddddddddddddd/}g }| jrt	| jddD ]*\}}|
 }||d< ||d< || qn"|
 }d|d< d|d< || |S )a  
        Convert to Shopify CSV format with multiple rows (one per image).
        
        Returns:
            List of dicts, each representing a row in Shopify CSV format.
            Each row has the same product data, but different Image Src and Image Position.
         -N   r4   ZTRUETitlezDefault TitleZshopifyZdenyZmanualZFALSE   newZkgZactive)/ZHandlerM   zBody (HTML)ZVendorr@   rC   rB   zOption1 NamezOption1 ValuezOption2 NamezOption2 ValuezOption3 NamezOption3 ValuezVariant SKUzVariant GramszVariant Inventory TrackerzVariant Inventory QtyzVariant Inventory PolicyzVariant Fulfillment ServicezVariant PricezVariant Compare At PricezVariant Requires ShippingzVariant TaxablezVariant BarcodezImage Alt Textz	Gift Cardz	SEO TitlezSEO Descriptionz)Google Shopping / Google Product CategoryzGoogle Shopping / GenderzGoogle Shopping / Age GroupzGoogle Shopping / MPNz"Google Shopping / AdWords Groupingz Google Shopping / AdWords LabelszGoogle Shopping / Conditionz Google Shopping / Custom Productz Google Shopping / Custom Label 0z Google Shopping / Custom Label 1z Google Shopping / Custom Label 2z Google Shopping / Custom Label 3z Google Shopping / Custom Label 4zVariant ImagezVariant Weight UnitzVariant Tax CodezCost per itemzIncluded / VietnamZStatusr%   )startz	Image SrczImage Position)r   r   lowerreplacer   r   r   r   r   	enumeratecopyappend)rF   ZhandleZbase_rowZrowsZpositionZ	image_urlrowr"   r"   r#   rH      sz     3
zProduct.to_shopify_csv_rows)*r   r   r   r   r	   r   r:   __annotations__r   r   floatr   r   r   r;   r   r   r   r   r   r   r   r   r   r   r   intr   r    r   nowr!   r
   classmethodr3   r5   r   r=   r7   rG   rI   rH   r"   r"   r"   r#   r$   %   s:   
 +r$   c                   @   sH   e Zd ZdZe Ze Ze Ze Z	e Z
e Ze ZdS )ShopItemzIScrapy item for shop/site information (aggregated across multiple pages).N)r   r   r   r   r   r	   Z	item_typebase_urldomainZpage_urlZ	page_typeZ	extractedr!   r"   r"   r"   r#   r\      s   r\   c                   @   s   e Zd ZU dZdZee ed< dZee ed< dZ	ee ed< dZ
ee ed< dZee ed< eedZee ed	< eedZee ed
< eedZee ed< eedZee ed< eedZee ed< eedZeeef ed< eejdZeed< dS )ShopzKPydantic model for aggregated shop/site info (best-effort, may be partial).Nr]   r^   r   r   logo)r(   emailsphonessocial_links	addressespagesrawr!   )r   r   r   r   r]   r   r:   rW   r^   r   r   r`   r	   r;   ra   r   rb   rc   rd   re   r   r7   rf   r   r   rZ   r!   r"   r"   r"   r#   r_      s   
r_   )r   r   typingr   r   r   r   r   Zpydanticr   r   r	   r
   ZItemr   r$   r\   r_   r"   r"   r"   r#   <module>   s    .