
from django.shortcuts import render, redirect

# Import User
from django.contrib.auth.models import User


#Importing the django login authentifications
from django.contrib.auth import login , logout
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required

# Iportant to serialize your data to a form understandable by rest frameworks.
#from .serlializers import NoteSerializer
from rest_framework import serializers

# Codes for rest_framework installation
from rest_framework import status, viewsets
from rest_framework.permissions import AllowAny
from rest_framework.decorators import action
from django.http import HttpResponse

from openai import OpenAI
import os

import requests
import json


class DummySerializer(serializers.Serializer):
    pass


client = OpenAI( api_key=os.getenv("OPENAI_API_KEY"),timeout=200 )

class InfoScrapperDefaultPage(viewsets.ViewSet):  # Changed from ModelViewSet to ViewSet
    permission_classes = (AllowAny,)
      
      
    @action(detail=False, methods=['get', 'POST'], url_path='home')
    def default_page(self, request, *args, **kwargs):
        base_domain = request.build_absolute_uri('/')
        results = False
        
        context = {
            'company_name': '',
            'company_url': '',
            'company_location': '',
            'results': False,
            'analysis_result': None,
            'result_color': '',
        }
            
        if request.method == 'POST':
                # Get form data
                company_name = request.POST.get('companyName', '').strip()
                company_url = request.POST.get('CompanywebUrl', '').strip()
                company_location = request.POST.get('CompanyLocation', '').strip()
                
                response_text = {}

                # Validate inputs
                if not all([company_name, company_url, company_location]):
                    print("Not all Colors Are Provided")
                    context.update({
                        'error': 'Please provide company name, URL, and location',
                        'result_color': 'red',
                    })
                    return render(request, 'index.html', context)

                # Initialize OpenAI client
                try:

                    url = "https://google.serper.dev/search"

                    payload = json.dumps({
                    "q": f"{company_name}"
                    })
                    headers = {
                    'X-API-KEY': '8c3eae5e92134e6f7ac791ed00ce85c5d6f3778e',
                    'Content-Type': 'application/json'
                    }

                    response_text = requests.request("POST", url, headers=headers, data=payload)
                    
                    prompt = self.build_prompt(response_text.text, company_name, company_url, company_location)
                    
                    response = client.chat.completions.create(
                        model="gpt-4",
                        messages=[{"role": "user", "content": prompt}],
                        temperature=0.0,
                        max_tokens=2000
                    )
                    
                    analysis_result = response.choices[0].message.content
                    result_color = self.determine_result_color(analysis_result)

                    context.update({
                        'company_name': company_name,
                        'company_url': company_url,
                        'company_location': company_location,
                        'results': True,
                        'analysis_result': analysis_result,
                        'result_color': result_color,
                    })
                           
                except Exception as e:
                    context.update({
                        'error': f"Failed to initialize OpenAI client: {str(e)}",
                        'result_color': 'red',
                    })
                    
                    return render(request, 'index.html', context)

        return render(request, 'index.html', context)
                    
    def build_prompt(self, response_text, company_name, company_url, company_location):
        
        return f"""
              Your task is to analyze the information provided {company_name}, {company_url} and {company_location}. check whether the information provided is accurate or not. If headquarters and year founded for {company_name} is provided then state it. IF you don't have access to online information, then use {response_text} as the data that you need to analyze, if headquarters and year founded are not available, say "Headquarter and Year founded NOT available". Confirm whether the {company_name}, {company_url} and {company_location} (optional) from the data. 
            If you find information provided is accurate the return green, if you find conflicting information return yellow.. if you find no information at all return red. If you find multiple information return grey.
            """

    def determine_result_color(self, analysis_result):
            """Determine the color code based on the analysis result content"""
            analysis_result = analysis_result.lower()
            
            if "red" in analysis_result:
                return "red"
            elif "yellow" in analysis_result or ("green" in analysis_result and "red" in analysis_result):
                return "yellow"
            elif "grey" in analysis_result in analysis_result:
                return "grey"
            else:
                return "green"

class InfoScrapperViewSet(viewsets.ViewSet):  # Changed from ModelViewSet to ViewSet
    permission_classes = (AllowAny,)
    serializer_class = DummySerializer
    
    @action(detail=False, methods=['get', 'POST'], url_path='analyze')
    def analyze(self, request):
        company_name = request.POST.get('companyName', None)
        company_url = request.POST.get('CompanywebUrl', None)
        company_location = request.POST.get('CompanyLocation', None)
        
        print(f"Company Name: {company_name}")
        print(f"Company URL: {company_url}")    
        print(f"Company Logo: {company_location}") 
        
        return render(request, 'index.html')