from models import Attendance
from datetime import timedelta

def sync_leave_to_attendance(db, employee_id, start_date, end_date, leave_type_name):
    curr_date = start_date
    while curr_date <= end_date:
        attn = Attendance.query.filter_by(employee_id=employee_id, date=curr_date).first()
        if not attn:
            attn = Attendance(
                employee_id=employee_id,
                date=curr_date,
                status='Leave',
                remarks=leave_type_name
            )
            db.session.add(attn)
        else:
            attn.status = 'Leave'
            attn.remarks = leave_type_name
        curr_date += timedelta(days=1)