from sqlalchemy.orm import Session
from src.marketing.apps.Calendar.model import Calendar
from src.marketing.apps.Calendar import schema
from src.utils.db import get_db
from fastapi import HTTPException, status
from typing import List, Optional
from src.marketing.apps.Calendar.service import create_calendar, delete_calendar,get_calendar_by_branch_id,update_calendar,get_calendar_by_id, get_all_timezones


# Create a new calendar
def create_calendar_controller(db: Session, calendar_data: schema.CalendarCreate) -> schema.CalendarOut:
    try:
        return create_calendar(db, calendar_data)
    except Exception as e:
        raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))




# Get a calendar by ID

def get_calendar_by_id_controller(db: Session, calendar_id: int) -> Optional[schema.CalendarOut]:
    try:
        result = get_calendar_by_id(db, calendar_id)
        if not result:
            raise HTTPException(
                status_code=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION,
                detail={
                    "success": False,
                    "message": "No calendars found for this ID",
                    "data": []
                }
            )
        return result
    except HTTPException as e:
        raise e
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail={
                "success": False,
                "message": "Internal server error",
                "data": str(e)
            }
        )



#------------------------------------------------------------------------------------------------------------------------
# Get a calendar by branch_id


def get_calendar_by_branch_id_controller(
    db: Session,
    branch_id: int,
    show_connected_accounts: bool = False,
    show_posts: bool = False,
    show_post_types: bool = False
):
    calendars = get_calendar_by_branch_id(
        db=db,
        branch_id=branch_id,
        show_connected_accounts=show_connected_accounts,
        show_posts=show_posts,
        show_post_types=show_post_types
    )
    
    if not calendars:
        raise HTTPException(
            status_code=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION,
            detail={
                "success": False,
                "message": "No calendars found for this branch",
                "data": []
            }
        )
    return calendars


#---------------------------------------------------------------------------------------------------------------------------

# # Update a calendar
def update_calendar_controller(db: Session, calendar_id: int, calendar_data: schema.CalendarUpdate) -> schema.CalendarOut:
    try:
        result  =  update_calendar(db, calendar_id, calendar_data)
        if not result:
            raise HTTPException(
                status_code=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION,
                detail={
                    "success": False,
                    "message": "No calendars found for this ID",
                    "data": []
                }
            )
        return result 
    except HTTPException as e:
        raise e
    except Exception as e:
        raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))
# Delete a calendar
def delete_calendar_controller(db: Session, calendar_id: int) -> None:
    try:
        delete_calendar(db, calendar_id)
    except HTTPException as e:
        raise e
    except Exception as e:
        raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))
    



# Get all timezones
def get_all_timezones_controller() -> List[str]:
    try:
        time_zones =  get_all_timezones()
        if not time_zones:
            raise HTTPException(
                status_code=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION,
                detail={
                    "success": False,
                    "message": "No timezones found",
                    "data": []
                }
            )
        return time_zones
    except Exception as e:
        raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))