#!/bin/bash
# Gantt Board Task CRUD Operations
# Usage: ./task-crud.sh [create|read|update|delete|list] [args...]

SUPABASE_URL="https://qnatchrjlpehiijwtreh.supabase.co"
SERVICE_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InFuYXRjaHJqbHBlaGlpand0cmVoIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc3MTY0MDQzNiwiZXhwIjoyMDg3MjE2NDM2fQ.rHoc3NfL59S4lejU4-ArSzox1krQkQG-TnfXb6sslm0"

HEADERS=(-H "apikey: $SERVICE_KEY" -H "Authorization: Bearer $SERVICE_KEY" -H "Content-Type: application/json")

function list_tasks() {
    local status_filter="${1:-}"
    local url="$SUPABASE_URL/rest/v1/tasks?select=*&order=created_at.desc"
    if [[ -n "$status_filter" ]]; then
        url="$SUPABASE_URL/rest/v1/tasks?select=*&status=eq.$status_filter&order=created_at.desc"
    fi
    curl -s "$url" "${HEADERS[@]}" | jq '.'
}

function get_task() {
    local task_id="$1"
    curl -s "$SUPABASE_URL/rest/v1/tasks?id=eq.$task_id&select=*" "${HEADERS[@]}" | jq '.[0]'
}

function create_task() {
    local title="$1"
    local status="${2:-open}"
    local priority="${3:-medium}"
    local project_id="${4:-1}"
    local assignee_id="${5:-9c29cc99-81a1-4e75-8dff-cd7cc5ceb5aa}"
    
    local uuid=$(uuidgen | tr '[:upper:]' '[:lower:]')
    local now=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
    
    curl -s -X POST "$SUPABASE_URL/rest/v1/tasks" \
        "${HEADERS[@]}" \
        -d "{
            \"id\": \"$uuid\",
            \"title\": \"$title\",
            \"status\": \"$status\",
            \"priority\": \"$priority\",
            \"project_id\": \"$project_id\",
            \"assignee_id\": \"$assignee_id\",
            \"created_at\": \"$now\",
            \"updated_at\": \"$now\",
            \"comments\": [],
            \"tags\": [],
            \"attachments\": []
        }" | jq '.'
    
    echo "Created task: $uuid"
}

function update_task() {
    local task_id="$1"
    local field="$2"
    local value="$3"
    local now=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
    
    curl -s -X PATCH "$SUPABASE_URL/rest/v1/tasks?id=eq.$task_id" \
        "${HEADERS[@]}" \
        -d "{\"$field\": \"$value\", \"updated_at\": \"$now\"}" | jq '.'
    
    echo "Updated task $task_id: $field = $value"
}

function delete_task() {
    local task_id="$1"
    curl -s -X DELETE "$SUPABASE_URL/rest/v1/tasks?id=eq.$task_id" "${HEADERS[@]}"
    echo "Deleted task: $task_id"
}

# Main
case "$1" in
    list)
        list_tasks "$2"
        ;;
    get)
        get_task "$2"
        ;;
    create)
        create_task "$2" "$3" "$4" "$5" "$6"
        ;;
    update)
        update_task "$2" "$3" "$4"
        ;;
    delete)
        delete_task "$2"
        ;;
    *)
        echo "Usage: $0 [list|get|create|update|delete] [args...]"
        echo ""
        echo "Examples:"
        echo "  $0 list                              # List all tasks"
        echo "  $0 list open                         # List open tasks"
        echo "  $0 get <task-id>                     # Get specific task"
        echo "  $0 create \"Task title\" open medium   # Create task"
        echo "  $0 update <task-id> status done      # Update task status"
        echo "  $0 delete <task-id>                  # Delete task"
        ;;
esac