import random
import string
from datetime import datetime, timedelta, UTC
from uuid import UUID
from fastapi import APIRouter, HTTPException
from fastapi.params import Depends
from sqlalchemy.orm import Session

from ocpp.v201.call import Reset, SetVariables

from app.database import get_db
from app.ocpp_proto import chargepoint_manager
from app.schemas.auth_token import AccessToken
from app.schemas.chargepoint import (
    ChargePoint,
    ChargePointCreate,
    ChargePointUpdate,
    ChargePointPassword,
    ChargePointConnectionInfo,
    ChargePointResetRequest,
    ChargePointResetResponse
)
from app.schemas.id_token import IdTokenLearnRequest, IdTokenLearnResponse
from app.schemas.chargepoint_variable import (
    ChargepointVariable,
    ChargepointVariableUpdate,
    ChargepointVariableResponse,
    MutabilityType,
    SetVariableStatusType
)
from app.models.chargepoint import ChargePoint as DbChargePoint
from app.models.user import User as DbUser
from app.models.chargepoint_variable import ChargepointVariable as DbChargepointVariable
from app.security.jwt_bearer import JWTBearer

router = APIRouter(
    prefix="/chargepoints",
    tags=["Chargepoint (v1)"],
)

@router.get(path="", response_model=list[ChargePoint])
async def get_chargepoints(
    skip: int = 0,
    limit: int = 20,
    db: Session = Depends(get_db),
    token: AccessToken = Depends(JWTBearer()),
):
    return db.query(DbChargePoint).order_by(DbChargePoint.identity).offset(skip).limit(limit).all()

@router.get(path="/{chargepoint_id}", response_model=ChargePoint)
async def get_chargepoint(
    chargepoint_id: UUID,
    db: Session = Depends(get_db),
    token: AccessToken = Depends(JWTBearer()),
):
    chargepoint = db.get(DbChargePoint, chargepoint_id)
    if chargepoint is None:
        raise HTTPException(status_code=404, detail="Chargepoint not found")
    return chargepoint

@router.get(path="/{chargepoint_id}/password", response_model=ChargePointPassword)
async def get_chargepoint_password(
    chargepoint_id: UUID,
    db: Session = Depends(get_db),
    token: AccessToken = Depends(JWTBearer(required_roles=["administrator"])),
):
    chargepoint = db.get(DbChargePoint, chargepoint_id)
    if chargepoint is None:
        raise HTTPException(status_code=404, detail="Chargepoint not found")
    return ChargePointPassword(password=chargepoint.password)

@router.delete(path="/{chargepoint_id}/password", response_model=ChargePointPassword)
async def reset_chargepoint_password(
    chargepoint_id: UUID,
    db: Session = Depends(get_db),
    token: AccessToken = Depends(JWTBearer(required_roles=["administrator"])),
):
    chargepoint = db.get(DbChargePoint, chargepoint_id)
    if chargepoint is None:
        raise HTTPException(status_code=404, detail="Chargepoint not found")
    chargepoint.password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(24))
    db.commit()
    return ChargePointPassword(password=chargepoint.password)

@router.post(path="", status_code=201, response_model=ChargePoint)
async def create_chargepoint(
    chargepoint: ChargePointCreate,
    db: Session = Depends(get_db),
    token: AccessToken = Depends(JWTBearer(required_roles=["administrator"])),
):
    chargepoint_db = DbChargePoint(
        identity=chargepoint.identity,
        is_active=chargepoint.is_active,
        password=''.join(random.choice(string.ascii_letters + string.digits) for i in range(24)),
        price=chargepoint.price
    )
    db.add(chargepoint_db)
    db.commit()
    db.refresh(chargepoint_db)
    return chargepoint_db

@router.patch(path="/{chargepoint_id}", response_model=ChargePoint)
async def update_chargepoint(
    chargepoint_id: UUID,
    chargepoint_update: ChargePointUpdate,
    db: Session = Depends(get_db),
    token: AccessToken = Depends(JWTBearer(required_roles=["administrator"])),
):
    chargepoint = db.get(DbChargePoint, chargepoint_id)
    if chargepoint is None:
        raise HTTPException(status_code=404, detail="Chargepoint not found")
    for key, value in chargepoint_update.model_dump(exclude_unset=True).items():
        setattr(chargepoint, key, value)
    db.commit()
    return chargepoint

@router.delete(path="/{chargepoint_id}", response_model=None)
async def delete_chargepoint(
    chargepoint_id: UUID,
    db: Session = Depends(get_db),
    token: AccessToken = Depends(JWTBearer(required_roles=["administrator"])),
):
    chargepoint = db.get(DbChargePoint, chargepoint_id)
    if chargepoint is None:
        raise HTTPException(status_code=404, detail="Chargepoint not found")
    db.delete(chargepoint)
    db.commit()
    return []

@router.get(path="/{chargepoint_id}/status", response_model=ChargePointConnectionInfo)
async def get_chargepoint_status(
    chargepoint_id: UUID,
    token: AccessToken = Depends(JWTBearer()),
):
    return ChargePointConnectionInfo(
        connected=chargepoint_manager.is_connected(chargepoint_id)
    )

@router.post(path="/{chargepoint_id}/reset", response_model=ChargePointResetResponse)
async def reset_chargepoint(
    chargepoint_id: UUID,
    reset_request: ChargePointResetRequest,
    token: AccessToken = Depends(JWTBearer(required_roles=["administrator"])),
):
    if chargepoint_manager.is_connected(chargepoint_id) == False:
        raise HTTPException(status_code=503, detail="Chargepoint not connected.")
    try:
        response = await chargepoint_manager.call(
            chargepoint_id,
            payload=Reset(type=reset_request.type, evse_id=reset_request.evse_id)
        )
        return ChargePointResetResponse(status=response.status)
    except TimeoutError:
        raise HTTPException(status_code=503, detail="Chargepoint didn't respond in time.")
    
@router.post(path="/{chargepoint_id}/token-learning", status_code=201, response_model=IdTokenLearnResponse)
async def create_id_token_learn_request(
    chargepoint_id: UUID,
    learn_request: IdTokenLearnRequest,
    db: Session = Depends(get_db),
    token: AccessToken = Depends(JWTBearer(required_roles=["administrator"])),
):
    chargepoint = db.get(DbChargePoint, chargepoint_id)
    if chargepoint is None:
        raise HTTPException(status_code=404, detail="Chargepoint not found")
    
    owner = db.get(DbUser, learn_request.user_id)
    if owner == None:
        raise HTTPException(status_code=422, detail=[{
            "loc": ["body", "user_id"],
            "msg": "Target user not found",
            "type": "invalid_relation" 
        }])
    
    chargepoint.learn_user_id = learn_request.user_id

    if learn_request.until == None:
        chargepoint.learn_until = datetime.now(UTC) + timedelta(minutes=5)
    else:
        chargepoint.learn_until = learn_request.until

    db.commit()

    return IdTokenLearnResponse(
        user_id=chargepoint.learn_user_id,
        until=chargepoint.learn_until
    )

@router.get(path="/{chargepoint_id}/token-learning", response_model=IdTokenLearnResponse)
async def get_id_token_learn_request(
    chargepoint_id: UUID,
    db: Session = Depends(get_db),
    token: AccessToken = Depends(JWTBearer(required_roles=["administrator"])),
):
    chargepoint = db.get(DbChargePoint, chargepoint_id)
    if chargepoint is None:
        raise HTTPException(status_code=404, detail="Chargepoint not found")
    
    if chargepoint.learn_user_id == None:
        raise HTTPException(status_code=404, detail="No active learning request")

    return IdTokenLearnResponse(
        user_id=chargepoint.learn_user_id,
        until=chargepoint.learn_until
    )

@router.delete(path="/{chargepoint_id}/token-learning", response_model=[])
async def get_id_token_learn_request(
    chargepoint_id: UUID,
    db: Session = Depends(get_db),
    token: AccessToken = Depends(JWTBearer(required_roles=["administrator"])),
):
    chargepoint = db.get(DbChargePoint, chargepoint_id)
    if chargepoint is None:
        raise HTTPException(status_code=404, detail="Chargepoint not found")
    
    if chargepoint.learn_user_id == None:
        raise HTTPException(status_code=404, detail="No active learning request")
    
    chargepoint.learn_user_id = None
    chargepoint.learn_until = None
    db.commit()

    return []

@router.get(path="/{chargepoint_id}/variables", response_model=list[ChargepointVariable])
async def get_chargepoint_variables(
    chargepoint_id: UUID,
    db: Session = Depends(get_db),
    token: AccessToken = Depends(JWTBearer(required_roles=["administrator"])),
):
    chargepoint = db.get(DbChargePoint, chargepoint_id)
    if chargepoint is None:
        raise HTTPException(status_code=404, detail="Chargepoint not found")
    
    return db.query(DbChargepointVariable).filter(DbChargepointVariable.chargepoint_id == chargepoint_id).all()

@router.put(path="/{chargepoint_id}/variables/{variable_id}", response_model=ChargepointVariableResponse)
async def update_chargepoint_variable(
    chargepoint_id: UUID,
    variable_id: UUID,
    variable_update: ChargepointVariableUpdate,
    db: Session = Depends(get_db),
    token: AccessToken = Depends(JWTBearer(required_roles=["administrator"])),
):
    chargepoint = db.get(DbChargePoint, chargepoint_id)
    if chargepoint is None:
        raise HTTPException(status_code=404, detail="Chargepoint not found")
    
    variable = db.query(DbChargepointVariable).filter(
        DbChargepointVariable.chargepoint_id == chargepoint_id,
        DbChargepointVariable.id == variable_id
    ).first()
    if variable is None:
        raise HTTPException(status_code=404, detail="ChargepointVariable not found")
    if variable.mutability == MutabilityType.READ_ONLY:
        raise HTTPException(status_code=422, detail="ChargepointVariable is read-only")
    
    variable.value = variable_update.value
    
    if chargepoint_manager.is_connected(chargepoint_id) == False:
        raise HTTPException(status_code=503, detail="Chargepoint not connected.")
    try:
        evse = None
        if variable.evse != None:
            evse = {
                'id': variable.evse
            }
            if variable.connector_id != None:
                evse['connectorId'] = variable.connector_id
        result = await chargepoint_manager.call(
            chargepoint_id,
            payload=SetVariables(set_variable_data=[
                {
                    'attributeType': variable.type.value,
                    'attributeValue': variable_update.value,
                    'component': {
                        'name': variable.component_name,
                        'instance': variable.component_instance,
                        'evse': evse
                    },
                    'variable': {
                        'name': variable.name
                    }
                }
            ])
        )
        status = result.set_variable_result[0]['attribute_status']
        if SetVariableStatusType(status) in [SetVariableStatusType.ACCEPTED, SetVariableStatusType.REBOOT_REQUIRED]:
            db.commit()
        else:
            raise HTTPException(status_code=500, detail=status)
        return ChargepointVariableResponse(status=status)
    except TimeoutError:
        raise HTTPException(status_code=503, detail="Chargepoint didn't respond in time.")