from django.shortcuts import render
from django.views.generic import CreateView
from aiohttp import request
import django.http as http
from mainview.models import *
import datetime
import logging 
from django.utils import timezone
# Vista principal del porfolio
from django.http import JsonResponse

from django.conf import settings
from django.shortcuts import render, redirect
from django.shortcuts import render, redirect
from django.views.generic.edit import FormView
from django.http import JsonResponse
from django.conf import settings

import json
from django.views.decorators.csrf import csrf_exempt

logging.basicConfig(
format='%(asctime)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',  #zona horaria (%Z)
level=logging.INFO)

class MainView(FormView):
    template_name = 'base_index.html'
    success_url = 'base_index.html'  # Agrega la URL de redirección en caso de éxito

    def get(self, request, *args, **kwargs):
        try:

            infoPorfolio = PorfolioInfo.objects.get(usuario__username="sirse")
            datosExtraUser = DatosExtraUser.objects.get(usuario__username="sirse")
            experiencia_laboral = Experiencia.objects.filter(usuario__username="sirse").order_by('-fecha_ini')
            skills = Skills.objects.filter(usuario__username="sirse")

        
            try:
                configuracionCSS = ConfiguracionCSS.objects.get(usuario__username="sirse")
            except ConfiguracionCSS.DoesNotExist:
                configuracionCSS = ConfiguracionCSS()
                configuracionCSS.usuario = User.objects.get(username="sirse")
                configuracionCSS.root_primary_color = "#fb4444"
                configuracionCSS.save()
                pass
            

            skillList =[]
            for skill in skills:
                skill.toJson()
                skillList.append(skill)
            info_log = {"infoPorfolio":infoPorfolio.toJson(),
                        "configuracionCSS":configuracionCSS.toJson(),
                        "skills":str(skillList)
                        }

            Loggin(info_log, "infoPorfolio")

            if not infoPorfolio.active:
                self.template_name = 'index.html'
            return render(request, self.template_name, {'infoPorfolio': infoPorfolio, 'datosExtraUser': datosExtraUser, 'experiencia_laboral': experiencia_laboral,  'skills':skills,'configuracionCSS': configuracionCSS})
        
        except Exception as e:
            logging.error(f'[ERROR] Error al invocar vista: {str(e)}', exc_info=True)
            return render(request, 'error.html', {'error_message': 'Ocurrió un error inesperado!'})
        


#TRAZAVILIDAD

def Loggin(info_log,name):
    try:
        logging.info("[INFO] time: %s" +name+":%s", timezone.now(),json.dumps(info_log))
    except Exception as e:
        logging.error(f'Error al trazar: {"info_log:",info_log,"name:",name,"Error:", str(e)}', exc_info=True)