from datetime import datetime
from sqlalchemy.orm import Session

from schemas.drivetask import CreateTask
from db.models.drivetask import DriveTask
from db.repository.user import get_user_by_id


def create_new_task(task: CreateTask, db: Session):
    print("Creating new task" + str(task) + " by user " + str(task.DriverId))
    driver = get_user_by_id(task.DriverId, "Driver", db)
    if not driver:
        return "notdriver"
    elif driver.Role != "Driver":
        return "notdriver"
    task_object = DriveTask(
        DriverId=task.DriverId,
        Description=task.Description,
        Status="Pending",
        StartLocation=task.StartLocation,
        EndLocation=task.EndLocation,
    )
    db.add(task_object)
    db.commit()
    db.refresh(task_object)
    return task_object


def change_task_status(task_id: int, status: str, distance: int, db: Session):
    print("Changing task status to " + status + " for task " + str(task_id))
    task = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
    if not task:
        return "notaskfound"
    if status == "In Progress":
        # see if there are any other tasks in progress by this driver, if yes, cancel
        task.StartDateTime = datetime.now()
    if status == "Completed":
        task.DistanceCovered = distance
        task.EndDateTime = datetime.now()
    task.Status = status
    db.commit()
    db.refresh(task)
    return task


def get_task_driver(task_id: int, db: Session):
    task = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
    if not task:
        return "notaskfound"
    driver = get_user_by_id(task.DriverId, "Any", db)
    if not driver:
        return "notdriver"
    return driver


def get_tasks_by_driver(driver_id: int, db: Session):
    driver = get_user_by_id(driver_id, "Any", db)
    if not driver:
        return "notdriver"
    if driver.Role != "Driver":
        return "notdriver"
    tasks = db.query(DriveTask).filter(DriveTask.DriverId == driver_id).all()
    return tasks


def get_task_by_id(task_id: int, db: Session):
    task = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
    if not task:
        return "notaskfound"
    task.Driver = task.CreatedBy.__dict__
    task.Driver["AssignedVehicle"] = task.CreatedBy.vehicle
    return task


def get_all_tasks(status: str, db: Session):
    if status != "Any":
        tasks = db.query(DriveTask).filter(DriveTask.Status == status).all()
    else:
        tasks = db.query(DriveTask).all()
    for task in tasks:
        task.Driver = task.CreatedBy.__dict__
        task.Driver["AssignedVehicle"] = task.CreatedBy.vehicle
    return tasks


def edit_task(task_id: int, task: CreateTask, db: Session):
    tasks = db.query(DriveTask).filter(DriveTask.Id == task_id).first()
    if not tasks:
        return "notaskfound"
    tasks.Description = task.Description
    tasks.StartLocation = task.StartLocation
    tasks.EndLocation = task.EndLocation
    db.commit()
    db.refresh(tasks)
    return tasks


def get_active_route_by_driver(driver_id: int, db: Session):
    driver = get_user_by_id(driver_id, "Driver", db)
    if not driver:
        return "notdriver"
    tasks = db.query(DriveTask).filter(DriveTask.DriverId == driver_id).all()
    for task in tasks:
        if task.Status == "In Progress":
            return task
    return "noroute"


def get_my_routes(driver_id: int, db: Session):
    driver = get_user_by_id(driver_id, "Driver", db)
    if not driver:
        return "notdriver"
    tasks = db.query(DriveTask).filter(DriveTask.DriverId == driver_id).all()
    return tasks