
    g5              
       X   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mZmZmZ d dlmZ d dlmZ d	ed
efdZd	ed
efdZ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fdZd	e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
efdZd	ededed
edef
dZd	eded
efdZ y)    )Session)IntegrityError)HTTPException)datetime)StoreBranch	StoreType)StoreCreateStoreResponseBranchCreateBranchResponse)get_current_user)Userdbcurrent_userc                     | j                  t              j                  t        j                  |j                  k(        j	                         S )z.
    Check if a user already has a store.
    queryr   filteruser_idfirstr   r   s     7/var/www/html/hubwallet-dev/src/apps/stores/services.pyget_store_by_userr      5     88E?!!%--<3G3G"GHNNPP    c                     | j                  t              j                  t        j                  |j                  k(        j	                         S )z.
    Check if a store exists by store_id.
    r   r   s     r   get_store_by_idr      r   r   
store_namec                     | j                  t              j                  t        j                  |k(        j	                         S )z"
    Get a store by its name.
    )r   r   r   namer   )r   r   s     r   get_store_by_namer"      s/     88E?!!%**
":;AACCr   storec                 L   | j                  t              j                  t        j                  |j                  k(        j	                         }|st        ddddd      t        |j                  |j                  |j                  d      }| j                  |       | j                          | j                  |       |j                  d   }t        |j                  |j                  |j                  |j                   |j"                  	      }| j                  |       | j                          | j                  |       |j                  |j                  |j                  |j$                  |j                  |j&                  j)                         |j*                  j)                         |j,                  |j                  |j                  |j                   |j"                  |j                  |j&                  j)                         |j*                  j)                         d
gdS )N  FzInvalid store type selected.statuscodemessagestatus_codedetail   )r   storetype_idr!   branch_limitr   )store_idr   branch_nameaddresscontact_info)	branch_idr0   r1   r2   r3   r   
created_at
updated_at)r0   r!   r.   r/   r   r5   r6   branches)r   r	   r   r.   r   r   r   r   r!   addcommitrefreshr7   r   r0   r1   r2   r3   r/   r5   	isoformatr6   r4   )r   r#   r   
store_type	new_storefirst_branch
new_branchs          r   create_storer@   "   s   )$++I,B,BeFXFX,XY__aJ#S=[\
 	
 $$''ZZ	I FF9IIKJJy >>!$L ##$$ ,,$$!..J FF:IIKJJz &&!..!..''**446**446 (11&//)55%-- * 7 7'//(33==?(33==?	
 r   new_store_namereturnc                    t        | |      }|st        ddddd      | j                  t              j	                  t        j
                  |k(  t        j                  |j                  k7        j                         rt        ddddd      ||_        t        j                         |_
        	 | j                          | j                  |       | j                  t              j	                  t        j                  |j                  k(        j!                         }|D cg c]R  }|j"                  |j                  |j$                  |j&                  |j(                  |j*                  |j                  d	T }}d
dd|j                  |j
                  |j,                  |j*                  |j                  |dgdS # t        $ r" | j                          t        ddddd      w xY wc c}w )z
    Update the store's name and updated_at timestamp based on the current user.
    Ensures store name uniqueness before updating.
      FzStore not foundr&   r*   r%   zStore name must be unique.z7Failed to update store name due to database constraint.)r4   r0   r1   r2   r3   r5   r6   T   zStore name updated successfully)r0   r!   r   r5   r6   r7   r'   r(   r)   data)r   r   r   r   r   r!   r0   r   r   utcnowr6   r9   r:   r   rollbackr   allr4   r1   r2   r3   r5   r   )r   rA   r   r#   r7   branchbranch_lists          r   update_store_namerM   ]   s    b,/E#S=NO
 	
 
xxejjN:ENNenn<\]cce#S=YZ
 	
  EJ(E

		


5 xx&&v%..'HIMMOH !
   ))!--~~"// ++ ++	

K 
 4 "NN

 ==#..#..'	
	 /  

#S=vw
 	


s   1!F= #AG+=+G(c           
      J   t        |t              st        ddddd      | j                  t              j                  t        j                  |j                  k(        j                         }|st        ddddd      	 | j                  t              j                  t        j                  |j                  k(        j                          | j                  |       | j                          dd	d
dS # t        $ r"}t        ddddt        |       d      d}~ww xY w)z
    Delete a store and all its associated branches.
    The store ID is automatically retrieved based on the authenticated user.
    r%   FzInvalid user objectr&   r*   rD   zStore not found or unauthorizedTrE   z6Store and all associated branches deleted successfully  z,An error occurred while deleting the store: N)
isinstancer   r   r   r   r   r   r   r   r0   deleter9   	Exceptionstr)r   r   r#   es       r   delete_storerU      s)    lD)uc]r4stt HHUO""5==L4H4H#HIOOQEuc]~4  A  	AX
5>> ABIIK
		%
		SU 	U  Xuc  aM  NQ  RS  NT  MU  _V  5W  X  	XXs   A3C7 7	D" DD"r0   r4   c                    | j                  t              j                  t        j                  |k(  t        j                  |j                  k(        j                         }|st        dd      | j                  t              j                  t        j                  |k(  t        j                  |k(        j                         }|st        dd      |S )zq
    Retrieve a branch using store_id and branch_id. Ensures the branch belongs to the current user's store.
    rD   z Store not found or unauthorized.r*   Branch not found.)	r   r   r   r0   r   r   r   r   r4   )r   r0   r4   r   r#   rK   s         r   get_branch_by_idrX      s     HHUO""5>>X#=u}}P\PdPd?dekkmE4VWWXXf$$V__%@&BRBRV_B_`ffhF4GHHMr   rK   c                    | j                  t              j                  t        j                  |j                  k(        j	                         }|st        ddddd      | j                  t              j                  t        j                  |j                  k(  t        j                  |j                  k(        j	                         }|rt        ddddd      t        |j                  |j                  |j                  |j                  |j                  t        j                         t        j                               }	 | j                  |       | j                          | j                  |       ddd|j$                  |j                  |j                  |j                  |j                  |j&                  |j(                  |j                  dgdS # t         $ r" | j#                          t        d	dd	d
d      w xY w)zH
    Create a new branch for the authenticated user using store_id.
    rD   FStore not found.r&   r*   r%   ,Branch name must be unique within the store.)r0   r1   r2   r3   r   r5   r6   rO   z,An error occurred while creating the branch.TrE   zBranch created successfullyr4   r0   r1   r2   r3   r5   r6   r   rF   )r   r   r   r   r   r   r   r0   r1   r2   r3   r   rH   r8   r9   r:   rR   rI   r4   r5   r6   )r   rK   r   r#   existing_branchr?   s         r   create_branchr^      s   
 HHUO""5==L4H4H#HIOOQE#S=OP
 	
 hhv&--5>>)f000 eg  #S=kl
 	
 &&(($$??$??$J	

z
		


: 0 (11&//)55%-- * 7 7(33(33%--	
	   

#S=kl
 	

s   2G +Hc                 n   | j                  t              j                  t        j                  |k(  t        j                  |j                  k(        j                         }|st        dd      | j                  t              j                  t        j                  |k(        j                         }|st        dd      |D cg c]e  }t        |j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                        g c}S c c}w )zc
    Retrieve all branches of a given store_id if the store belongs to the authenticated user.
    rD   z/Store not found or does not belong to the user.r*   z!No branches found for this store.r\   )r   r   r   r0   r   r   r   r   rJ   r   r4   r1   r2   r3   r5   r6   )r   r0   r   r#   r7   rK   s         r   get_branches_by_store_idr`     s     HHUO""("--- eg 

 4effxx&&v('BCGGIH4WXX   	&&__**NN,,((((NN		
  s   A*D2c                    | j                  t              j                  t        j                  |j                  k(        j	                         }|st        ddddd      | j                  t              j                  t        j                  |j                  k(  t        j                  |k(        j	                         }|st        ddddd      | j                  t              j                  t        j                  |j                  k(  t        j                  |j                  k(        j	                         }|r*|j                  |j                  k7  rt        ddddd      |j                  |_	        |j                  |_
        |j                  |_        t        j                         |_        	 | j                          | j!                  |       ddd|j                  |j                  |j                  |j                  |j                  |j&                  |j                  |j                  dgdS # t"        $ r" | j%                          t        d	dd	d
d      w xY w)z`
    Update a branch using branch_id. Ensures the branch belongs to the authenticated user.
    rD   FrZ   r&   r*   rW   r%   r[   rO   z,An error occurred while updating the branch.TrE   zBranch updated successfullyr\   rF   )r   r   r   r   r   r   r   r0   r4   r1   r2   r3   r   rH   r6   r9   r:   rR   rI   r5   )r   r4   rK   r   r#   branch_to_updater]   s          r   update_branchrc   .  s(    HHUO""5==L4H4H#HIOOQE#S=OP
 	

 xx'..v%../PRXRbRbfoRopvvx#S=PQ
 	

 hhv&--foo.OQWQcQcgmgygyQyz  A  A  CO?448H8R8RR#S=kl
 	
 $*#5#5 %~~$*$7$7!"*//"3

		


#$ 0 .77,55/;;+33 0 = =.99.99+33	
	   

#S=kl
 	

s   5!H7 7+I"c                 R   | j                  t              j                  t        j                  |j                  k(        j	                         }|st        ddddd      | j                  t              j                  t        j                  |j                  k(  t        j                  |k(        j	                         }|st        ddddd      	 | j                  |       | j                          d	d
ddS # t        $ r" | j                          t        ddddd      w xY w)z`
    Delete a branch using branch_id. Ensures the branch belongs to the authenticated user.
    rD   FrZ   r&   r*   rW   rO   z,An error occurred while deleting the branch.TrE   zBranch deleted successfully.)r   r   r   r   r   r   r   r0   r4   rQ   r9   rR   rI   )r   r4   r   r#   branch_to_deletes        r   delete_branchrf   j  s    HHUO""5==L4H4H#HIOOQEuc]o4pqqxx'..v%../PRXRbRbfoRopvvxuc]p4qrrN
		"#
		
 13 3	  N
uc  ^L  5M  N  	NNs   !C; ;+D&N)!sqlalchemy.ormr   sqlalchemy.excr   fastapir   r   src.apps.stores.modelsr   r   r	   src.apps.stores.schemasr
   r   r   r   src.apps.auth.controllerr   src.apps.users.modelsr   r   r   rS   r"   r@   dictrM   rU   rX   r^   r`   rc   rf    r   r   <module>rp      sa   " ) !  ; ; \ \ 5 &Q' Q QQ Qd QD' Ds D7W 7[ 7 7v@' @3 @d @t @FXW XD X@ C C t =g =| =4 =D =~ C t @9g 9# 9| 9SW 9\` 9x3g 3# 3T 3r   