# Path: alertaclient/utils.py
# class DateTime:
#     @staticmethod
#     def parse(date_str):
#         if not isinstance(date_str, str):
#             return
#         try:
#             return datetime.datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S.%fZ')
#         except Exception:
#             raise ValueError('dates must be ISO 8601 date format YYYY-MM-DDThh:mm:ss.sssZ')
# 
#     @staticmethod
#     def iso8601(dt):
#         return dt.replace(microsecond=0).strftime('%Y-%m-%dT%H:%M:%S') + '.%03dZ' % (dt.microsecond // 1000)
# 
#     @staticmethod
#     def localtime(dt, timezone=None, fmt='%Y/%m/%d %H:%M:%S'):
#         tz = pytz.timezone(timezone)
#         try:
#             return dt.replace(tzinfo=pytz.UTC).astimezone(tz).strftime(fmt)
#         except AttributeError:
#             return
# 
# def build_query(filters):
#     return [tuple(f.split('=', 1)) for f in filters if '=' in f]


import json
import click
from tabulate import tabulate
from alertaclient.models.alert import Alert
from alertaclient.utils import DateTime, build_query



COLOR_MAP = {
    'critical': {'fg': 'red'},
    'major': {'fg': 'magenta'},
    'minor': {'fg': 'yellow'},
    'warning': {'fg': 'blue'},
    'normal': {'fg': 'green'},
    'indeterminate': {'fg': 'cyan'},
}


@click.command('query', short_help='Search for alerts')
@click.option('--ids', '-i', metavar='ID', multiple=True, help='List of alert IDs (can use short 8-char id)')
@click.option('--query', '-q', 'query', metavar='QUERY', help='severity:"warning" AND resource:web')
@click.option('--filter', '-f', 'filters', metavar='FILTER', multiple=True, help='KEY=VALUE eg. serverity=warning resource=web')
@click.option('--oneline', 'display', flag_value='oneline', default=True, help='Show alerts using table format')
@click.option('--medium', 'display', flag_value='medium', help='Show important alert attributes')
@click.option('--full', 'display', flag_value='full', help='Show full alert details')
@click.pass_obj
def cli(obj, ids, query, filters, display, from_date=None):
    """Query for alerts based on search filter criteria."""
    client = obj['client']
    timezone = obj['timezone']
    if ids:
        query = [('id', x) for x in ids]
    elif query:
        query = [('q', query)]
    else:

# Path: alertaclient/utils.py
# class DateTime:
#     @staticmethod
#     def parse(date_str):
#         if not isinstance(date_str, str):
#             return
#         try:
#             return datetime.datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S.%fZ')
#         except Exception:
#             raise ValueError('dates must be ISO 8601 date format YYYY-MM-DDThh:mm:ss.sssZ')
# 
#     @staticmethod
#     def iso8601(dt):
#         return dt.replace(microsecond=0).strftime('%Y-%m-%dT%H:%M:%S') + '.%03dZ' % (dt.microsecond // 1000)
# 
#     @staticmethod
#     def localtime(dt, timezone=None, fmt='%Y/%m/%d %H:%M:%S'):
#         tz = pytz.timezone(timezone)
#         try:
#             return dt.replace(tzinfo=pytz.UTC).astimezone(tz).strftime(fmt)
#         except AttributeError:
#             return
# 
# def build_query(filters):
#     return [tuple(f.split('=', 1)) for f in filters if '=' in f]


# Path: alertaclient/commands/cmd_query.py
import json
import click
from tabulate import tabulate
from alertaclient.models.alert import Alert
from alertaclient.utils import DateTime, build_query




COLOR_MAP = {
    'critical': {'fg': 'red'},
    'major': {'fg': 'magenta'},
    'minor': {'fg': 'yellow'},
    'warning': {'fg': 'blue'},
    'normal': {'fg': 'green'},
    'indeterminate': {'fg': 'cyan'},
}


@click.command('query', short_help='Search for alerts')
@click.option('--ids', '-i', metavar='ID', multiple=True, help='List of alert IDs (can use short 8-char id)')
@click.option('--query', '-q', 'query', metavar='QUERY', help='severity:"warning" AND resource:web')
@click.option('--filter', '-f', 'filters', metavar='FILTER', multiple=True, help='KEY=VALUE eg. serverity=warning resource=web')
@click.option('--oneline', 'display', flag_value='oneline', default=True, help='Show alerts using table format')
@click.option('--medium', 'display', flag_value='medium', help='Show important alert attributes')
@click.option('--full', 'display', flag_value='full', help='Show full alert details')
@click.pass_obj
def cli(obj, ids, query, filters, display, from_date=None):
    """Query for alerts based on search filter criteria."""
    client = obj['client']
    timezone = obj['timezone']
    if ids:
        query = [('id', x) for x in ids]
    elif query:
        query = [('q', query)]
    else:

        query = build_query(filters)