from fastapi import APIRouter, Form, Depends, HTTPException, File, UploadFile
from typing import List,Optional,Union
from fastapi.responses import FileResponse
from random import randint
from fastapi.security import OAuth2PasswordRequestForm
from app.models.models import Guidance_group
from app.api import deps
from sqlalchemy.orm import Session
from typing import Any, Dict
import secrets
from fastapi_login.exceptions import InvalidCredentialsException
from fastapi_login import LoginManager
from datetime import timedelta,datetime
from jose import jwt
from emails.template import JinjaTemplate
from tortoise.queryset import Q
from fastapi.responses import HTMLResponse

guidance = APIRouter()

@guidance.get("/get_guidance_group")
async def get_guidance_group(
    guidance_group_id : Optional[int] = None
):
    try:
        if guidance_group_id :
            news_list = await Guidance_group.filter(id=guidance_group_id).all()
        else:    
            news_list = await Guidance_group.all()

        return {"msg": "success", "code": 200, "Guidance_groups": news_list}
    except Exception as e:
        return {"msg": str(e), "code": 500}

@guidance.post("/insert_guidance_group")
async def insert_news(
    name : str = Form(default=''),
    contact_person : str = Form(default=''),
    contact_phone :str = Form(default=''),
    line : int = Form(default=0),
    remark : str = Form(default=''),
    address : str = Form(default=''),
    email : str = Form(default=''),
    information : str = Form(default=''),
    request : str = Form(default=''),
    schedule : str = Form(default=''),
    result : str = Form(default=''),
    person : str = Form(default=''),
    create_time :str = Form(default=datetime.now())
):
    try:

        new_news = await Guidance_group.create(
            name=name,
            contact_person=contact_person,
            contact_phone=contact_phone,
            line = line,
            email = email,
            remark =remark,
            address = address,
            information = information,
            request = request,
            schedule = schedule ,
            result = result,
            person = person,
            create_time = create_time
        )
        
        return {"msg": "success", "code": 200, "new_Guidance_group": new_news.id}
    except Exception as e:
        return {"msg": str(e), "code": 500}

@guidance.post("/update_guidance_group")
async def update_news(
    guidance_group_id : int = Form(default=None),
    name : str = Form(default=''),
    contact_person : str = Form(default=''),
    contact_phone :str = Form(default=''),
    line : int = Form(default=None),
    remark : str = Form(default=''),
    address : str = Form(default=''),
    email : str = Form(default=''),
    information : str = Form(default=''),
    request : str = Form(default=''),
    schedule : str = Form(default=''),
    result : str = Form(default=''),
    person : str = Form(default='')
):
    try:
        if not guidance_group_id :
            return {"msg": "no ID"}
        
        news_obj = await Guidance_group.get(id=guidance_group_id)

        if name.strip() != '':
            news_obj.name = name
        
        if contact_person.strip() != '':
            news_obj.contact_person = contact_person

        if contact_phone.strip() != '':
            news_obj.contact_phone = contact_phone

        if line != None:
            news_obj.line = line

        if email != None:
            news_obj.email = email

        if information != None:
            news_obj.information = information

        if request != None:
            news_obj.request = request

        if schedule != None:
            news_obj.schedule = schedule

        if result != None:
            news_obj.result = result

        if person != None:
            news_obj.person = person

        if remark.strip() != '':
            news_obj.remark = remark

        if address.strip() != '':
            news_obj.address = address
        
        await news_obj.save()
        
        return {"msg": "success", "code": 200}
    except Exception as e:
        return {"msg": str(e), "code": 500}

@guidance.post("/delete_guidance_group")
async def delete_guidance_group(guidance_group_id: Optional[int] = None):
    if guidance_group_id :
        await Guidance_group.filter(id=guidance_group_id).delete()
        return {"msg": "success", "code": 200}
    else :
        return {"msg": "please input ID", "code": 200}