import arcpy
import os

# Define the path for the input shapefile and output shapefile directory
input_shapefile = "C:/Users/BrianT/RIT/TEACHING/IGME_386_C_drive/Week_12_SDS_Predictive_Models/prompts/data_output/prompt_1_data_subset.shp"  # Placeholder path for the input shapefile; update with the actual path
output_folder = "C:/temp"
output_shapefile = os.path.join(output_folder, "prompt_3_hotspots.shp")

try:
    # Check if the input shapefile exists before proceeding
    if not arcpy.Exists(input_shapefile):
        raise FileNotFoundError(f"The input shapefile was not found at {input_shapefile}. Please check the path.")

    # Set environment settings
    arcpy.env.workspace = output_folder
    arcpy.env.overwriteOutput = True  # Allow overwriting of existing files

    # Print feedback for the user
    print("Starting Hot Spot Analysis (Getis-Ord Gi*) on the input shapefile...")

    # Run the Hot Spot Analysis (Getis-Ord Gi*) with specified parameters
    arcpy.stats.HotSpots(
        Input_Feature_Class=input_shapefile,             # Input shapefile for analysis
        Input_Field="Vol",                               # Field to analyze (Vol)
        Output_Feature_Class=output_shapefile,           # Output shapefile location
        Conceptualization_of_Spatial_Relationships="FIXED_DISTANCE_BAND",  # Spatial relationship type
        Distance_Band_or_Threshold_Distance=4584.50,      # Fixed distance threshold of 1000 units
        Distance_Method="MANHATTAN_DISTANCE"             # Distance method: Manhattan distance
    )

    # Inform the user that the process is complete
    print("Hot Spot Analysis complete. Results saved to:", output_shapefile)

except arcpy.ExecuteError:
    # Handle geoprocessing errors (specific to ArcPy functions)
    print("ArcPy error:", arcpy.GetMessages(2))

except FileNotFoundError as e:
    # Handle file not found error for the input shapefile
    print(str(e))

except Exception as e:
    # Handle any other unexpected errors
    print("An unexpected error occurred:", str(e))

finally:
    # Final statement to confirm the script has completed execution
    print("Script execution complete.")
