
    Jh                        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	 d dl
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 d dlmZ dededefdZ	 	 ddededefdZdededefdZdedededefdZdededdfdZy)    )datetime)UUID)Session)HTTPExceptionstatus)APIException)Country)settings)API_PREFIXES)CountryCreateSchemaCountryUpdateSchemaCountryOutSchema)QueryPaginator)Regiondb
country_idreturnc                   K   	 | j                  t              j                  t        j                  |k(  t        j                  j                  d             j                         }|st        dddit        j                  d      |S # t        $ r-}t        ddt        |      it        j                  d      d }~ww xY ww)Nr	   	exceptionzCountry not foundzCountry not found.moduleerrorstatus_codemessagezError fetching country by ID.)queryr	   filterid
deleted_atis_firstr   r   HTTP_404_NOT_FOUND	ExceptionstrHTTP_500_INTERNAL_SERVER_ERRORr   r   countryes       X/var/www/html/wine-match-dev/backend/winematch-backend/src/apps/base/services/country.pyget_country_by_idr)      s     
((7#**JJ*$""4(
 %' 	  "$78"55,	   
A'==3	
 	

s)   CBB C	B>(B99B>>Cpageper_pagec           
        K   	 |dz
  |z  }| j                  t              j                  t        j                  j	                  d             }t        |t        dj                  t        t        j                               t        j                  g      ||d      }|j                         S # t        $ r-}t        ddt        |      it         j"                  d      d }~ww xY ww)	N    T)r   schemaurloffsetlimituse_ormget_all_countriesr   zFailed to fetch countries.r   )r   r	   r   r   r   r   r   joinr#   r
   api_base_urlr   COUNTRYpaginater"   r   r   r$   )r   r*   r+   r1   r   	paginatorr'   s          r(   r4   r4   )   s     

(h&!((););)?)?)EF"#X2245|7K7KLM
	 !!## 
&A'==0	
 	

s)   C%B'B, +C%,	C"5(CC""C%payloadc                   K   	 | j                  t              j                  t        j                  |j                  k(        j	                         }|r|j                  U	 |j                  |_        d |_
        t        j                         |_        | j                          | j                  |       |S t        dddit        j                   d      	 t        d	i |j#                         }| j%                  |       | j                          | j                  |       |S # t
        $ r-}t        ddt        |      it        j                  d      d }~ww xY w# t
        $ r-}t        ddt        |      it        j                  d      d }~ww xY w# t
        $ r-}t        ddt        |      it        j                  d      d }~ww xY ww)
Nr	   r   z$Error checking for existing country.r   z%Error restoring soft-deleted country.zCountry already exists.z&Country with this name already exists.zError creating country. )r   r	   r   namer    r"   r   r#   r   r$   r   r   utcnow
updated_atcommitrefreshHTTP_400_BAD_REQUESTdictadd)r   r:   existingr'   r&   s        r(   create_countryrF   D   s    

88G$++\\W\\)

%' 	 * '&*#&.oo&7#		

8$  "$=>"77@	 
+GLLN+
w
		


7O  
A'==:	
 	

$  "$&A/ & E EC	 ,  
A'==-	
 	

ss   GAD! G AE 3 GAF  G!	E*(EEG	F#(FFG	G	(GG		Gc                 ^  K   t        | |       d {   }	 |j                  d      }|j                         D ]  \  }}t        |||        | j	                          | j                  |       |S 7 _# t        $ r-}t        ddt        |      it        j                  d      d }~ww xY ww)NT)exclude_unsetr	   r   zError updating country.r   )r)   rC   itemssetattrr@   rA   r"   r   r#   r   r$   )r   r   r:   r&   update_datafieldvaluer'   s           r(   update_countryrN   |   s     %b*55G
lll6'--/ 	+LE5GUE*	+ 			


7 6  
A'==-	
 	

s3   B-A2B-AA4 1B-4	B*=(B%%B**B-Nc                 .  K   t        | |       d {   }	 d|_        t        j                         |_        | j                          | j                  |       y 7 G# t        $ r-}t        ddt        |      it        j                  d      d }~ww xY ww)NTr	   r   zError deleting country.r   )r)   
is_deletedr   r>   r   r@   rA   r"   r   r#   r   r$   r%   s       r(   delete_countryrQ      s     %b*55G
!%__.
		


7) 6*  
A'==-	
 	

s3   BABAA B	B%(BBB)r-   
   )r   uuidr   sqlalchemy.ormr   fastapir   r   src.core.exceptionsr   src.apps.base.models.countryr	   src.core.configr
   src.utils.constantsr   src.apps.base.schemas.countryr   r   r   src.utils.paginationr   src.apps.base.models.regionsr   intr)   r4   rF   rN   rQ   r<       r(   <module>r_      s      " ) , 0 $ , 
 0 /
 
S 
W 
2 



 
64
W 4
/B 4
w 4
p
W 
# 
@S 
X_ 
*
W 
# 
$ 
r^   