from fastapi import APIRouter, HTTPException, File, UploadFile
import requests
from pydantic import BaseModel
from minio import Minio
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.utils import ImageReader
import io
from datetime import datetime
#SuperToken Auth from front end
from supertokens_python.recipe.session.framework.fastapi import verify_session
from supertokens_python.recipe.session import SessionContainer
from fastapi import Depends


router = APIRouter()

class medCertUploud(BaseModel):
    fullName: str 
    docfname: str 
    startDate: str 
    endDate: str 
    returnDate: str 

#=================understand Supertoken multirequest for file submission================================
# Get List of all files by patient
@router.post("/files/upload/file/", tags="patients_files")
async def generateAndUploudMedCert( file: UploadFile = File(...)):
    extension = file.filename.split(".")
    print(file.file)
    print(file.filename)
    print(extension)
    print(file.size)
    uploudFile(file.filename, extension[1], file.file, file.size)
    
    return {"message": "Successfully Uploaded File"}

# Get List of all files by patient
@router.post("/files/generate/med-cert/", tags="patients_files")
async def generateAndUploudMedCert(requestItem: medCertUploud, session: SessionContainer = Depends(verify_session())):
    uploudMedCert(requestItem.fullName, 
               requestItem.docfname,
               requestItem.startDate,
               requestItem.endDate,
               requestItem.returnDate)
    return {"message": "Successfully Generated File"}

def uploudFile(fileName, extension, content, size):
    client = Minio("minio:9000",   
    access_key="user1",
    secret_key="C@rtoon1995",
    secure=False
    )
    found = client.bucket_exists("mih")
    if not found:
        client.make_bucket("mih")
    else:
        print("Bucket already exists")
    client.put_object("mih", 
                    fileName, 
                    content, 
                    length=size,
                    content_type=f"application/{extension}",)
        


#"minio""localhost:9000"
def uploudMedCert(fullName, docfname, startDate, endDate, returnDate):
    client = Minio("minio:9000",   
    access_key="user1",
    secret_key="C@rtoon1995",
    secure=False
    )
    generateMedCertPDF(fullName, docfname, startDate, endDate, returnDate)
    found = client.bucket_exists("mih")
    if not found:
        client.make_bucket("mih")
    else:
        print("Bucket already exists")
    fileName = f"Med-Cert-{fullName}-{startDate}.pdf"
    client.fput_object("mih", fileName, "temp.pdf")

def generateMedCertPDF(fullName, docfname, startDate, endDate, returnDate):
    w,h = A4
    today = datetime.today().strftime('%d-%m-%Y')
    myCanvas = canvas.Canvas("temp.pdf", pagesize=A4)
    myCanvas.setFont('Helvetica', 12)
    myCanvas.drawString(w - 100,h - 50,today)

    myCanvas.setFont('Helvetica-Bold', 20)
    myCanvas.drawString(w-375, h - 100, "Medical Certificate")

    myCanvas.setFont('Helvetica', 12)
    line1 = "This is to certify that " + fullName.upper() + " was seen by " + docfname.upper() + " on " + startDate + "."
    line2 = "He/She is unfit to attend work/school from " + startDate + " up to and including " + endDate + "."
    line3 = "He/She will return on " + returnDate + "."
    myCanvas.drawString(50, h-150,line1)
    myCanvas.drawString(50, h-180,line2)
    myCanvas.drawString(50, h-210,line3)

    myCanvas.line(50,h-430,200,h-430)
    myCanvas.drawString(50, h-450, docfname.upper())

    qrText = fullName.upper() + " booked off from " + startDate + " to " + endDate + " by " + docfname.upper() + ".\nPowered by Mzansi Innovation Hub."
    qrText = qrText.replace(" ","+")
    
    url = f"https://api.qrserver.com/v1/create-qr-code/?data={qrText}&size=100x100"
    response = requests.get(url)
    image = ImageReader(io.BytesIO(response.content))
    myCanvas.drawImage(image,50, h-700,100,100)

    myCanvas.setFont('Helvetica-Bold', 15)
    myCanvas.drawString(50,h-720,"Scan to verify")

    myCanvas.save()
    
#Def get
#uploudFile("Yasien Meth","Dr D Oct","18-06-2024","20-06-2024","21-06-2024")