import cv2
import numpy as np
import os
import datetime
import csv

# Load OpenCV Face Recognition and Haar Cascade for detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")

try:
    recognizer = cv2.face.LBPHFaceRecognizer_create()
except AttributeError:
    print("Error: OpenCV contrib package is missing.")
    print("Install with: pip install opencv-contrib-python")
    exit()

# File Paths
FACE_DIR = "registered_faces"
LOG_FILE = "attendance_log.csv"
MODEL_FILE = "face_recognizer.yml"
USER_ID_FILE = "last_user_id.txt"

def save_last_user_id(user_id):
    with open(USER_ID_FILE, "w") as file:
        file.write(str(user_id))

def get_last_user_id():
    if os.path.exists(USER_ID_FILE):
        with open(USER_ID_FILE, "r") as file:
            return file.read().strip()
    return None

def collect_training_data(user_id):
    """Captures face images for training."""
    if not user_id.isdigit():
        print("Error: User ID must be a number.")
        return

    user_folder = os.path.join(FACE_DIR, user_id)
    os.makedirs(user_folder, exist_ok=True)

    cap = cv2.VideoCapture(0)
    count = 0

    while count < 20:  # Capture 20 images per user
        ret, frame = cap.read()
        if not ret:
            break

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(50, 50))

        for (x, y, w, h) in faces:
            face_img = gray[y:y + h, x:x + w]
            resized_face = cv2.resize(face_img, (100, 100))  # Resize to a standard size
            cv2.imwrite(os.path.join(user_folder, f"{count}.jpg"), resized_face)
            count += 1

        cv2.imshow("Face Registration", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
    save_last_user_id(user_id)
    print(f"Training data collected for user {user_id}.")

def train_face_recognizer():
    """Trains LBPH face recognizer."""
    faces = []
    labels = []

    for user_id in os.listdir(FACE_DIR):
        if not user_id.isdigit():
            continue  # Skip non-user folders
        user_folder = os.path.join(FACE_DIR, user_id)

        for filename in os.listdir(user_folder):
            img_path = os.path.join(user_folder, filename)
            img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)

            if img is None:
                continue  # Skip unreadable files

            resized_face = cv2.resize(img, (100, 100))  # Resize again to avoid inconsistency
            faces.append(resized_face)
            labels.append(int(user_id))

    if not faces:
        print("No training data found.")
        return

    recognizer.train(faces, np.array(labels))
    recognizer.save(MODEL_FILE)
    print("Training complete.")

def recognize_user():
    """Recognizes a user using the trained model."""
    if not os.path.exists(MODEL_FILE):
        print("Error: Model file not found. Train the model first.")
        return

    recognizer.read(MODEL_FILE)
    cap = cv2.VideoCapture(0)

    while True:
        ret, frame = cap.read()
        if not ret:
            break

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(50, 50))

        for (x, y, w, h) in faces:
            face_img = gray[y:y + h, x:x + w]
            resized_face = cv2.resize(face_img, (100, 100))  # Resize for consistency

            user_id, confidence = recognizer.predict(resized_face)

            if confidence < 75:  # Lower threshold to improve recognition accuracy
                text = f"EMP #: {user_id} (Confidence: {round(confidence, 2)})"
                color = (0, 255, 0)
                log_attendance(user_id)
            else:
                text = "NEED TO REGISTER"
                color = (0, 0, 255)

            cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
            cv2.putText(frame, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2, cv2.LINE_AA)

        cv2.imshow("Face Recognition", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

def log_attendance(user_id):
    """Logs attendance for recognized users in CSV format."""
    now = datetime.datetime.now()
    today = now.strftime("%Y-%m-%d")
    timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
    log_data = []

    # Ensure CSV file exists
    if not os.path.exists(LOG_FILE):
        with open(LOG_FILE, "w", newline="") as file:
            writer = csv.writer(file)
            writer.writerow(["Timestamp", "User ID", "Attendance"])  # Write header

    # Read existing logs
    with open(LOG_FILE, "r") as file:
        reader = csv.reader(file)
        next(reader, None)  # Skip header
        log_data = list(reader)

    # Check if the user already checked in today
    user_logs_today = [log for log in log_data if log[1] == str(user_id) and log[0].startswith(today)]

    if not user_logs_today:
        log_data.append([timestamp, user_id, "Checkin"])
        print(f"✅ Check-in logged for User {user_id}")
    elif len(user_logs_today) == 1 and user_logs_today[0][2] == "Checkin":
        log_data.append([timestamp, user_id, "Checkout"])
        print(f"✅ Checkout logged for User {user_id}")
    else:
        return

    # Write updated logs
    with open(LOG_FILE, "w", newline="") as file:
        writer = csv.writer(file)
        writer.writerow(["Timestamp", "User ID", "Attendance"])  # Re-write header
        writer.writerows(log_data)

if __name__ == "__main__":
    while True:
        choice = input("1: Register User\n2: Train Model\n3: Recognize Face\n4: Exit\nChoose an option: ")
        if choice == "1":
            user_id = input("Enter user ID for registration: ")
            collect_training_data(user_id)
        elif choice == "2":
            train_face_recognizer()
        elif choice == "3":
            recognize_user()
        elif choice == "4":
            break
        else:
            print("Invalid choice. Try again.")
