from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Shop

class ShopListView(APIView):
    """
    View to list all shops with their details.
    """

    def get(self, request):
        try:
            shops = Shop.objects.filter(status='Open')
            shop_list = []

            for shop in shops:
                shop_list.append({
                    "unit_name": shop.unit_name,
                    "unit_location": shop.unit_location,
                    "address": {
                        "street": shop.street,
                        "city": shop.city,
                        "district": shop.district,
                        "state_or_province": shop.state_or_province,
                        "pin_code": shop.pin_code,
                    },
                    "latitude": shop.latitude,
                    "longitude": shop.longitude,
                    "contact_no": shop.contact_no,
                    "email": shop.email,
                })

            return Response(
                {
                    "status": 1,
                    "message": "Shops retrieved successfully.",
                    "shops": shop_list,
                },
                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,
            )