#!/bin/bash

# Week 1 Day 4-5: Create 20 Saved Filters
# This script runs AFTER all projects are created

# Atlassian Cloud credentials (hardcoded for reliable sourcing in SSH contexts)
ATLASSIAN_SITE_URL="https://ai-impress.atlassian.net"
JIRA_AUTH="di5zYW1vaWxlbmtvQGFpLWltcHJlc3MuY29tOkFUQVRUM3hGZkdGMEFCbEppNE1PUkoxTWx5YjZaaFItSTk4NFRZQ3JVaG9HSDN5SGYwYmpoWktrSl9wazI3czZtcmItR1ZvVnRyMGJYbVhXdFlEeVF0MFFMMFV0NkJOcG1mcnoxQVRKbklicUFzV1Z1V2VLSHhxeUtKOGdaVkFwc2k4T0JjLWpDMkJWb0c5VFVFQkRQRE1XbUdfMEpHM3pGVTZidjhqVG1HZWN3ZTJ4bFp6VGlKbz1FMDA5MzE0MA=="

echo "=== Week 1 Day 4-5: Creating 20 Saved Filters ==="
echo ""

# Function to create filter
create_filter() {
    local name=$1
    local jql=$2
    local description=$3
    local favourite=${4:-false}

    echo "Creating filter: $name..."

    # Use REST API v3 for filters - correct endpoint
    local json_data="{\"name\": \"$name\", \"description\": \"$description\", \"jql\": \"$jql\", \"favourite\": $favourite}"

    curl -s -X POST \
      "${ATLASSIAN_SITE_URL}/rest/api/3/filter" \
      -H "Authorization: Basic ${JIRA_AUTH}" \
      -H "Content-Type: application/json" \
      -d "$json_data" > /tmp/filter_response.json

    if grep -q "\"id\":" /tmp/filter_response.json; then
        FILTER_ID=$(grep -o '"id":[0-9]*' /tmp/filter_response.json | head -1 | cut -d':' -f2)
        echo "✓ $name создан (ID: $FILTER_ID)"
    else
        ERROR=$(grep -o '"message":"[^"]*"' /tmp/filter_response.json | head -1 | cut -d'"' -f4)
        if [ -z "$ERROR" ]; then
            ERROR=$(cat /tmp/filter_response.json | head -c 150)
        fi
        echo "❌ Ошибка: $ERROR"
    fi
    echo ""
}

# PROD Filters (4)
echo "--- PROD Project Filters ---"
create_filter \
    "PROD - My Tasks" \
    "project = PROD AND assignee = currentUser() AND status != Done" \
    "Tasks assigned to me in PROD" \
    "true"

create_filter \
    "PROD - Website Bugs" \
    "project = PROD AND type = Bug AND component = Website-Frontend" \
    "Bugs in website frontend" \
    "true"

create_filter \
    "PROD - This Sprint" \
    "project = PROD AND sprint in openSprints()" \
    "Issues in current sprint" \
    "true"

create_filter \
    "PROD - Blockers" \
    "project = PROD AND priority = Highest AND status != Done" \
    "Blocking issues" \
    "true"

# MARK Filters (5)
echo "--- MARK Project Filters ---"
create_filter \
    "MARK - All Leads" \
    "project = MARK AND type = Lead ORDER BY created DESC" \
    "All leads in pipeline" \
    "true"

create_filter \
    "MARK - My Leads" \
    "project = MARK AND type = Lead AND assignee = currentUser()" \
    "Leads assigned to me" \
    "true"

create_filter \
    "MARK - Opportunities" \
    "project = MARK AND type = Opportunity AND status != Lost" \
    "Active opportunities" \
    "true"

create_filter \
    "MARK - Clients" \
    "project = MARK AND type = Client" \
    "All clients" \
    "true"

create_filter \
    "MARK - Hot Leads" \
    "project = MARK AND type = Lead AND priority in (Highest, High)" \
    "High priority leads" \
    "true"

# SUPP Filters (3)
echo "--- SUPP Project Filters ---"
create_filter \
    "SUPP - Open Tickets" \
    "project = SUPP AND status != Closed ORDER BY priority DESC" \
    "All open support tickets" \
    "true"

create_filter \
    "SUPP - My Tickets" \
    "project = SUPP AND assignee = currentUser() AND status != Closed" \
    "Tickets assigned to me" \
    "true"

create_filter \
    "SUPP - Unassigned" \
    "project = SUPP AND assignee is EMPTY AND status != Closed" \
    "Unassigned support tickets" \
    "true"

# OPS Filters (4)
echo "--- OPS Project Filters ---"
create_filter \
    "OPS - Pending Invoices" \
    "project = OPS AND type = Invoice AND status != Paid" \
    "Unpaid invoices" \
    "true"

create_filter \
    "OPS - Overdue Invoices" \
    "project = OPS AND type = Invoice AND due < now() AND status != Paid" \
    "Past due invoices" \
    "true"

create_filter \
    "OPS - Contracts Pending" \
    "project = OPS AND type = Contract AND status = 'Pending Signature'" \
    "Contracts awaiting signature" \
    "true"

create_filter \
    "OPS - Pending Approval" \
    "project = OPS AND status = 'Pending Approval'" \
    "Items pending approval" \
    "true"

# Executive Filters (4)
echo "--- Executive Dashboard Filters ---"
create_filter \
    "EXEC - All Critical" \
    "priority = Highest AND status != Done ORDER BY created DESC" \
    "All critical priority issues" \
    "true"

create_filter \
    "EXEC - Revenue Pipeline" \
    "project = MARK AND type = Opportunity AND status not in (Lost, Won)" \
    "Active revenue opportunities" \
    "true"

create_filter \
    "EXEC - Overdue All" \
    "due < now() AND status not in (Done, Closed, Paid)" \
    "All overdue items" \
    "true"

create_filter \
    "EXEC - Burndown" \
    "project = PROD AND sprint in openSprints()" \
    "Current sprint burndown" \
    "true"

echo "✓ Filters creation complete (20 total)"
echo ""
echo "Next: Configure automation rules via UI"
