from django.shortcuts import render
from rest_framework.response import Response
from rest_framework import status
from .models import AboutUs, ContactUs, AppUrl, FAQ, PrivacyPolicy, TermsAndConditions
from rest_framework.views import APIView


# Create your views here.
class ListAboutUsContactUsAppURLView(APIView):
    def get(self, request):
        try:
            about_us = AboutUs.objects.first()  
            privacy = PrivacyPolicy.objects.first()
            terms = TermsAndConditions.objects.first()
            contact_us = ContactUs.objects.first()  
            app_url = AppUrl.objects.first()
            data = {
                "about_us": about_us.content if about_us else "",
                "image" : about_us.image.url if about_us.image else "",
                
                "privacy": privacy.content if privacy else "",
                "terms": terms.content if terms else "",
                "contact_us": {
                    "phone_number": contact_us.phone_number if contact_us else "",
                    "email": contact_us.email if contact_us else "",
                },
                "app_url": {
                    "playstore_link": app_url.playstore_link if app_url else "",
                    "appstore_link": app_url.appstore_link if app_url else "",
                },
            }

            return Response(
                {
                    "status": 1,
                    "message": "About Us, Contact Us, and App URL details retrieved successfully!",
                    "data": data,
                },
                status=status.HTTP_200_OK,
            )
        except Exception as e:
            return Response(
                {
                    "status": 0,
                    "message": "An unexpected error occurred.",
                    "exception": str(e),
                },
                status=status.HTTP_400_BAD_REQUEST,
            )


class ListFAQView(APIView):
    def get(self, request):
        try:
            faqs = FAQ.objects.all()

            faq_data = [
                {
                    "category": faq.category,
                    "question": faq.question,
                    "answer": faq.answer,
                }
                for faq in faqs
            ]

            return Response(
                {
                    "status": 1,
                    "message": "FAQs retrieved successfully!",
                    "data": faq_data,
                },
                status=status.HTTP_200_OK,
            )
        except Exception as e:
            return Response(
                {
                    "status": 0,
                    "message": "An unexpected error occurred.",
                    "exception": str(e),
                },
                status=status.HTTP_400_BAD_REQUEST,
            )
