#!/bin/bash

RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
RESET=$(tput sgr0)

usage() {
    echo "${BLUE}Usage:${RESET} $0 [--org <org>] [--team <team_slug>]"
    echo "  --org <org>         GitHub organization/account (or will prompt)"
    echo "  --team <team_slug>  Team slug (or will prompt)"
    echo ""
    echo "GITHUB_TOKEN must be set in your environment."
}

# Parse arguments
ORG=""
TEAM_SLUG=""
while [[ $# -gt 0 ]]; do
    case $1 in
        --org) ORG="$2"; shift 2;;
        --team) TEAM_SLUG="$2"; shift 2;;
        -h|--help) usage; exit 0;;
        *) echo "${RED}Unknown argument: $1${RESET}"; usage; exit 1;;
    esac
    done

if [[ -z "$GITHUB_TOKEN" ]]; then
    echo "${RED}❌ GITHUB_TOKEN is not set in your environment.${RESET}"
    exit 1
fi

# Prompt for org if not provided
if [[ -z "$ORG" ]]; then
    read -p "Enter GitHub organization/account: " ORG
fi

# Prompt for team slug if not provided
if [[ -z "$TEAM_SLUG" ]]; then
    read -p "Enter team slug: " TEAM_SLUG
fi

read -p "Are you sure you want to delete team '$TEAM_SLUG' from org '$ORG'? (yes/no): " CONFIRM
if [[ "$CONFIRM" != "yes" ]]; then
    echo "${YELLOW}Aborted by user.${RESET}"
    exit 0
fi

echo "${BLUE}Attempting to delete team '$TEAM_SLUG' from organization '$ORG'...${RESET}"
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE \
    -H "Authorization: token $GITHUB_TOKEN" \
    "https://api.github.com/orgs/$ORG/teams/$TEAM_SLUG")

if [[ "$RESPONSE" -eq 204 ]]; then
    echo "${GREEN}✅ Successfully deleted team '$TEAM_SLUG' from '$ORG'.${RESET}"
elif [[ "$RESPONSE" -eq 404 ]]; then
    echo "${YELLOW}⚠️ Team '$TEAM_SLUG' not found in organization '$ORG'.${RESET}"
else
    echo "${RED}❌ Failed to delete team '$TEAM_SLUG' from '$ORG' (HTTP Status: $RESPONSE).${RESET}"
fi 