
    sZh                     `    S SK r S SKrS SKrS SKrS SKrS SKJr  SrS rS r	S r
S rS rS	 rg)
    N)datetimezapp/facial_attendance_db.dbc                     [         R                  R                  [        5      (       d  [	        S5        [
        R                  " [        5       n U R                  5       nUR                  S5        UR                  S5        UR                  5        Vs/ s H  o"S   PM	     nnSU;  a  UR                  S5        SU;  a  UR                  S5        UR                  S	5        UR                  S
5        UR                  5        Vs/ s H  o"S   PM	     nnSU;  a  UR                  S5        UR                  S5        UR                  S5        UR                  5        Vs/ s H  o"S   PM	     nnSU;  a  UR                  S5        U R                  5         SSS5        gs  snf s  snf s  snf ! , (       d  f       g= f)zMSets up the database and creates necessary tables or migrates them if needed.z Creating facial_attendance_db...a  
            CREATE TABLE IF NOT EXISTS USERS (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL,
                email TEXT,
                employee_code TEXT,
                UNIQUE(name, email, employee_code)
            )
        zPRAGMA table_info(USERS)   emailz'ALTER TABLE USERS ADD COLUMN email TEXTemployee_codez/ALTER TABLE USERS ADD COLUMN employee_code TEXTa,  
            CREATE TABLE IF NOT EXISTS attendance (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                user_id INTEGER,
                timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY(user_id) REFERENCES USERS(id) ON DELETE CASCADE
            )
        zPRAGMA table_info(attendance)user_idz1ALTER TABLE attendance ADD COLUMN user_id INTEGERa8  
            CREATE TABLE IF NOT EXISTS USER_IMAGES (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                user_id INTEGER,
                image_path TEXT,
                encoding TEXT NOT NULL,
                FOREIGN KEY(user_id) REFERENCES USERS(id) ON DELETE CASCADE
            )
        zPRAGMA table_info(USER_IMAGES)z2ALTER TABLE USER_IMAGES ADD COLUMN user_id INTEGERN)ospathexistsAPP_DB_FILEprintsqlite3connectcursorexecutefetchallcommit)connr   rowcolumnss       7D:\DownLoads\facial_attendance_system-main\app\utils.pydb_setupr   
   sw   77>>+&&01		%  	 	12%+__%67%6cq6%67'!NNDE')NNLM 	  	 	67%+__%67%6cq6%67G#NNNO 	  	 	78%+__%67%6cq6%67G#NNOPa 
&	% 8  8 8[ 
&	%s9   AF/F !A$F/F%AF/ F*.)F/ F//
F=c                     [         R                  R                  U 5      (       aH  [        R                  " U 5      n[        R
                  " U5      nU(       a  US   $ [        SU  S35        g[        SU  S35        g)z"Encodes the face in an image file.r   zWarning: No faces found in 'z'.zError: Image file '' not found.N)r	   r
   r   face_recognitionload_image_fileface_encodingsr   )img_pathimg_dataimg_encodingss      r   
encode_imgr!   @   sq    	ww~~h#33H=(77A ##,XJb9:  	#H:\:;    c                     / n  [         R                  " [        5       nUR                  5       nUR	                  S5      R                  5       nU HJ  n[        R                  " [        R                  " US   5      5      nU R                  US   US   U45        ML     SSS5        U $ ! , (       d  f       U $ = f! [         a  n[        SU 35         SnAU $ SnAff = f)z&Loads all encodings from the database.z$SELECT id, name, encoding FROM USERS   r   r   NzError loading encodings: )r   r   r   r   r   r   nparrayjsonloadsappend	Exceptionr   )encoding_listsr   r   resultsr   encodinges          r   load_encodingsr/   L   s    N/__[)T[[]Fnn%KLUUWG88DJJs1v$67%%s1vs1vx&@A  *  *)   /)!-../s5   B: B B(B: (
B72B: 7B: :
CCCc                     [         R                  " [        5       nUR                  5       nUR	                  SU 45        UR                  5       nU(       a\  US   n[        R                  " 5       R                  S5      nUR	                  SXE45        UR                  5         [        SU  S35        O[        SU  S35        S	S	S	5        g	! , (       d  f       g	= f! [         R                   a  n[        S
U 35         S	nAg	S	nAff = f)z"Marks attendance for a given user.z!SELECT id FROM USERS WHERE name=?r   z%Y-%m-%d %H:%M:%Sz9INSERT INTO attendance (user_id, timestamp) VALUES (?, ?)zAttendance marked for .zUser 'r   NzError marking attendance: )r   r   r   r   r   fetchoner   nowstrftimer   r   Error)	user_namer   r   r   r   
local_timer.   s          r   mark_attendancer8   Z   s    0__[)T[[]FNN>M//#Ca&%\\^445HI
Z]d\qr.yk;<yk67 *)) == 0*1#.//0s5   C B&CC 
CC C D0DDc                      [         R                  " [        5       n U R                  5       nUR	                  S5      R                  5       nUsSSS5        $ ! , (       d  f       / $ = f! [         a  n[        SU 35         SnA/ $ SnAff = f)z1Fetches all attendance records with user details.z
                SELECT u.name, u.employee_code, a.timestamp 
                FROM attendance a 
                JOIN USERS u ON a.user_id = u.id 
                ORDER BY a.timestamp DESC
            Nz Error fetching attendance logs: )r   r   r   r   r   r   r*   r   )r   r   r,   r.   s       r   fetch_attendance_logsr:   m   s    6__[)T[[]Fnn & 
    *)) I  60455I6s4   A) 1A	A) 
A&!A) &A) )
B3BBc                     [         R                  " [        5       nUR                  5       nUR	                  SU 45      R                  5       nU HJ  u  nU(       d  M  [        R                  R                  U5      (       d  M4  [        R                  " U5        ML     UR	                  SU 45        UR	                  SU 45        UR	                  SU 45        UR                  5         SSS5        g! , (       d  f       g= f)z7Deletes a user and all their data (images, attendance).z2SELECT image_path FROM USER_IMAGES WHERE user_id=?z'DELETE FROM USER_IMAGES WHERE user_id=?z&DELETE FROM attendance WHERE user_id=?zDELETE FROM USERS WHERE id=?N)r   r   r   r   r   r   r	   r
   r   remover   )r   r   r   imagesr   s        r   delete_userr>   }   s    		% TW^V`ajjl!KXxBGGNN844		(# " 	@7*M?'L5zB 
&	%	%s   >C/"C/A#C//
C=)r	   r   r'   r   numpyr%   r   r   r   r!   r/   r8   r:   r>    r"   r   <module>rA      s;    	     +4l
0& r"   