
    Ji                         d dl mZ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  e       Zej                  d       ee
      fded	efd
       ZdededefdZy)    )	APIRouterDepends)Session)text)datetimetimezone)get_db)ActivityCategorizerz	/api/sync	sync_datadbc                   K   	 | j                  d      }| j                  dg       }| j                  d      }|j                  t        d      d|i      j                         }|sddiS |d   }t	               }d}d}	|D ]  }
	 |
j                  di       }|
j                  dd      }|d	k  r/|
j                  d|      }|j                  d
d      }|j                  d|j                  dd            }|j                  dd      }|j                  dd      }dD ]  }|j                  |d      } |j                  ||      }t        d|dd  d|        |r|j                  d      sd|j                         v rdnddd}t        ||      }t        |t              r&t        j                  |j                  dd            }n|}t        d      }|j                  |||dd  |dd! |r|dd" nd|r|dd" ndt        |      ||d   ||d#   t        j                  t         j"                        d$       |d%z  } |j'                          t        d'| d(| d)|	 d*       d+t)        |      ||	|d,S # t$        $ r}|	d%z  }	t        d&|        Y d}~d}~ww xY w# t$        $ r6}|j+                          t        d-|        t        |      d.d/cY d}~S d}~ww xY ww)0z8Sync endpoint saving all activities including duplicatestokendata	timestampz<SELECT developer_id FROM developers WHERE api_token = :tokenerrorzInvalid tokenr   duration   titleUntitledappapplicationUnknownurl file)z - Google Chromez - Mozilla Firefoxz - Microsoft Edgez - Visual Studio CodezDEBUG: 'N2   z' -> categorychromebrowseruncategorizedgeneral)r   subcategoryZz+00:00aL  
                    INSERT INTO activity_records (
                        developer_id, application_name, window_title,
                        url, file_path, duration, timestamp,
                        category, project_name, project_type,
                        created_at
                    ) VALUES (
                        :developer_id, :application_name, :window_title,
                        :url, :file_path, :duration, :timestamp,
                        :category, :project_name, :project_type,
                        :created_at
                    )
                   i  i  r"   )developer_idapplication_namewindow_titler   	file_pathr   r   r   project_nameproject_type
created_at   zError processing event: u   ✅ Synced z activities for z
 (failed: )T)successreceivedsavedfailed	developeru   ❌ Sync error: F)r   r.   )getexecuter   fetchoner
   replaceget_detailed_categoryprintlowerextract_project_name
isinstancestrr   fromisoformatintnowr   utc	Exceptioncommitlenrollback)r   r   r   r   r   r2   r%   categorizersaved_countfailed_countevent
event_datar   event_timestampr'   app_namer   r(   suffixcategory_infor)   parsed_timestampinsert_queryes                           9E:\timesheet\timesheet_new\backend\fixed_sync_endpoint.pyreceive_sync_datarR      s)    d3g&}}VR(MM+.	 JJOPe
 (* 	 _-- |)+EB"YYvr2
 99Z3a<"'))K"C)~~gzB%>>%y1YZ nnUB/&NN626	 wF#/#7#7#CL w !, A A,PX Ycr!2 35HI %M,=,=j,I19X^^=M1MISb'0%M  4L(K os3'/'='=o>U>UVY[c>d'e$'6$  $ %   

<$0(0#$0#$6),3u:"5>5D!1B #H!1 -j 9$0$1-$@"*,,x||"<*  q } J 			K=(8jQ]P^^_`aD	 "%
 	
  !045  3
 $%QE223sx   KA$J (K)J )I1,J -FI188J 0K1	J:JJ JJ 	K$+KKKKKr'   rK   returnc                    dd l }|j                  d|       }|r|j                  d      j                         S |j                  d|       }|r3t	        fddD              r|j                  d      j                         S |j                  d|       }|r|j                  d      S |j                  d|       }|r|j                  d      S |j                  d	|       }|r|j                  d      S y
)Nr   z2 - ([^-]+) - (?:Visual Studio Code|VS Code|Cursor)r,   u   ^([^–]+) – c              3   B   K   | ]  }|j                         v   y w)N)r9   ).0iderK   s     rQ   	<genexpr>z'extract_project_name.<locals>.<genexpr>{   s      fBe3shnn&66Bes   )intellijpycharmwebstormz\\([^\\]+)\\\.gitzgithub\.com/[^/]+/([^/\s]+)z\\([^\\]+)\\[^\\]+\.[a-z]+$r!   )researchgroupstripany)r'   rK   r\   vscode_matchjetbrains_match	git_match
repo_matchfolder_matchs    `      rQ   r:   r:   u   s    99RT`aL!!!$**,,ii 2LAO3fBeff$$Q'--//		.=Iq!!9<HJ""99;\JL!!!$$    N)fastapir   r   sqlalchemy.ormr   
sqlalchemyr   r   r   databaser	   activity_categorizerr
   routerpostdictrR   r<   r:    rf   rQ   <module>rp      sm    & "  '  4	[;B6? f3t f3 f3 f3Rs c c rf   