"""
CLI commands for tailmatter operations.
"""

import click
import json
from pathlib import Path
from .parser import TailmatterParser


@click.command('tailmatter-get')
@click.argument('key')
@click.option('--file', 'file_path', required=True, type=click.Path(exists=True),
              help='Path to markdown file')
@click.option('--format', 'output_format', default='raw', type=click.Choice(['raw', 'json']),
              help='Output format (raw or json)')
def tailmatter_get(key, file_path, output_format):
    """Get specific tailmatter value by key (supports dot notation for nested values)."""
    try:
        file_path = Path(file_path)
        with open(file_path, 'r', encoding='utf-8') as f:
            text = f.read()

        parser = TailmatterParser()
        value = parser.get_tailmatter_value(text, key)

        if value is None:
            click.echo(f"Key '{key}' not found in tailmatter", err=True)
            return

        if output_format == 'json':
            click.echo(json.dumps(value, indent=2))
        else:
            if isinstance(value, (dict, list)):
                click.echo(json.dumps(value, indent=2))
            else:
                click.echo(str(value))

    except Exception as e:
        click.echo(f"Error: {e}", err=True)
        raise click.ClickException(f"Failed to get tailmatter value from {file_path}")


@click.command('tailmatter-set')
@click.argument('key_value')
@click.option('--file', 'file_path', required=True, type=click.Path(exists=True),
              help='Path to markdown file')
@click.option('--backup', is_flag=True, help='Create backup of original file')
def tailmatter_set(key_value, file_path, backup):
    """Set tailmatter value (format: key=value, supports dot notation for nested)."""
    try:
        if '=' not in key_value:
            raise click.ClickException("Key-value must be in format 'key=value'")

        key, value = key_value.split('=', 1)
        key = key.strip()
        value = value.strip()

        # Try to parse value as JSON for complex types
        try:
            if value.lower() in ['true', 'false']:
                value = value.lower() == 'true'
            elif value.replace('.', '').replace('-', '').isdigit():
                value = float(value) if '.' in value else int(value)
            elif value.startswith('[') or value.startswith('{'):
                value = json.loads(value)
        except (json.JSONDecodeError, ValueError):
            pass

        file_path = Path(file_path)

        if backup:
            backup_path = file_path.with_suffix(f"{file_path.suffix}.bak")
            backup_path.write_text(file_path.read_text())
            click.echo(f"Backup created: {backup_path}")

        with open(file_path, 'r', encoding='utf-8') as f:
            text = f.read()

        parser = TailmatterParser()
        new_text = parser.set_tailmatter_value(text, key, value)

        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(new_text)

        click.echo(f"Set {key}={value} in tailmatter for {file_path}")

    except Exception as e:
        click.echo(f"Error: {e}", err=True)
        raise click.ClickException(f"Failed to set tailmatter value in {file_path}")


@click.command('tailmatter-keys')
@click.option('--file', 'file_path', required=True, type=click.Path(exists=True),
              help='Path to markdown file')
@click.option('--format', 'output_format', default='list', type=click.Choice(['list', 'json']),
              help='Output format (list or json)')
def tailmatter_keys(file_path, output_format):
    """List all tailmatter keys."""
    try:
        file_path = Path(file_path)
        with open(file_path, 'r', encoding='utf-8') as f:
            text = f.read()

        parser = TailmatterParser()
        keys = parser.get_tailmatter_keys(text)

        if not keys:
            click.echo("No tailmatter keys found")
            return

        if output_format == 'json':
            click.echo(json.dumps(keys, indent=2))
        else:
            for key in sorted(keys):
                click.echo(key)

    except Exception as e:
        click.echo(f"Error: {e}", err=True)
        raise click.ClickException(f"Failed to list tailmatter keys from {file_path}")


@click.command('tailmatter-stats')
@click.option('--file', 'file_path', required=True, type=click.Path(exists=True),
              help='Path to markdown file')
@click.option('--format', 'output_format', default='json', type=click.Choice(['json', 'text']),
              help='Output format (json or text)')
def tailmatter_stats(file_path, output_format):
    """Calculate tailmatter statistics."""
    try:
        file_path = Path(file_path)
        with open(file_path, 'r', encoding='utf-8') as f:
            text = f.read()

        parser = TailmatterParser()
        stats = parser.calculate_tailmatter_stats(text)

        if output_format == 'json':
            click.echo(json.dumps(stats.to_dict(), indent=2))
        else:
            click.echo(f"Has tailmatter: {stats.has_tailmatter}")
            click.echo(f"Format: {stats.format or 'N/A'}")
            click.echo(f"Total fields: {stats.total_fields}")
            click.echo(f"QA items: {stats.qa_items}")
            click.echo(f"QA completed: {stats.qa_completed}")
            click.echo(f"Editorial status: {stats.editorial_status or 'N/A'}")
            click.echo(f"Has agent config: {stats.has_agent_config}")

    except Exception as e:
        click.echo(f"Error: {e}", err=True)
        raise click.ClickException(f"Failed to calculate tailmatter stats for {file_path}")


@click.command('tailmatter-check')
@click.option('--file', 'file_path', required=True, type=click.Path(exists=True),
              help='Path to markdown file')
def tailmatter_check(file_path):
    """Run QA checklist validation."""
    try:
        file_path = Path(file_path)
        with open(file_path, 'r', encoding='utf-8') as f:
            text = f.read()

        parser = TailmatterParser()
        tailmatter = parser.extract_tailmatter(text)

        qa_checklist = tailmatter.get("qa_checklist", [])
        if not qa_checklist:
            click.echo("No QA checklist found in tailmatter")
            return

        click.echo("QA Checklist Status:")
        click.echo("=" * 50)

        total_items = len(qa_checklist)
        completed_items = 0

        for i, item in enumerate(qa_checklist, 1):
            if isinstance(item, dict):
                requirement = item.get("requirement", f"Item {i}")
                complete = item.get("complete", False)

                status_icon = "✅" if complete else "❌"
                click.echo(f"{status_icon} {requirement}")

                if complete:
                    completed_items += 1

        click.echo("=" * 50)
        click.echo(f"Progress: {completed_items}/{total_items} ({completed_items/total_items*100:.1f}%)")

        if completed_items == total_items:
            click.echo("🎉 All QA items completed!")
        else:
            click.echo(f"⚠️  {total_items - completed_items} items remaining")

    except Exception as e:
        click.echo(f"Error: {e}", err=True)
        raise click.ClickException(f"Failed to check QA status for {file_path}")