import os
import re
import sys
from datetime import datetime
from mysql.connector import Error

# Add the parent directory to the Python path to allow for absolute imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from services.mysql_service import get_db_connection
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

def parse_and_update_logs(log_file, conn):
    """
    Parses the log file and updates existing records in the attendance_logs table
    with the similarity score, if it's currently NULL.
    """
    log_pattern = re.compile(
        r"(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}):\s"
        r"(?:✅|Γ£à)\sSUCCESSFUL\sMATCH(?: & LOG)?:\s"
        r"User\sID:\s(\d+),\s"
        r"Name:\s([^,]+),\s"
        r"Similarity:\s([\d.]+)"
    )

    cursor = None
    try:
        cursor = conn.cursor()
        update_query = """
            UPDATE attendance_logs
            SET similarity = %s
            WHERE pid = %s
            AND recognition_timestamp = %s
            AND similarity IS NULL
        """
        
        print(f"Starting to parse log file: {log_file} to update similarity scores.")
        records_to_update = []
        
        with open(log_file, 'r', encoding='utf-8') as f:
            for line in f:
                match = log_pattern.search(line)
                if match:
                    timestamp_str, user_id_str, _, similarity_str = match.groups()
                    
                    timestamp = datetime.fromisoformat(timestamp_str)
                    user_id = int(user_id_str)
                    similarity = float(similarity_str)
                    
                    records_to_update.append((
                        similarity,
                        user_id,
                        timestamp
                    ))

        if records_to_update:
            print(f"Found {len(records_to_update)} potential records to update in the log file.")
            cursor.executemany(update_query, records_to_update)
            conn.commit()
            print(f"Successfully updated {cursor.rowcount} records in the 'attendance_logs' table with similarity scores.")
        else:
            print("No matching log entries found to process.")

    except Error as e:
        print(f"Database error during update: {e}")
        conn.rollback()
    except FileNotFoundError:
        print(f"Error: Log file not found at {log_file}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")
    finally:
        if cursor:
            cursor.close()

def main():
    """Main function to orchestrate the log parsing and database update process."""
    log_file_path = os.environ.get('LOG_FILE_PATH', os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), '2025-10-22-out.log'))
    
    # Validate that the log_file_path is actually a file
    if not os.path.isfile(log_file_path):
        print(f"Error: The specified log path is not a file or does not exist: {log_file_path}")
        return

    conn = None
    try:
        conn = get_db_connection()
        if conn:
            parse_and_update_logs(log_file_path, conn)
    finally:
        if conn and conn.is_connected():
            conn.close()
            print("Database connection closed.")

if __name__ == '__main__':
    main()