import depoc
import click
import sys

from typing import Any

from ..utils._response import _handle_response
from ..utils._format import _format_response


client = depoc.DepocClient()


@click.group
def category() -> None:
    ''' Manage Financial Categories '''
    pass

@category.command
@click.argument('name')
@click.option('--parent', help='Inform the Parent Caregory if any.')
def create(name: str, parent: str) -> None:
    ''' Create a new category. '''
    data: dict[str, Any] = {'name': name}
    data.update({'parent': parent}) if parent else None

    service = client.financial_categories.create

    if obj := _handle_response(service, data):
        highlight = f'{obj.parent.name}' if obj.parent.name else ''
        _format_response(obj, obj.name, highlight)

@category.command
@click.argument('id')
def get(id: str) -> None:
    ''' Retrieve an specific category '''
    service = client.financial_categories.get

    if obj := _handle_response(service, resource_id=id):
            highlight = f'{obj.parent.name}' if obj.parent.name else ''
            _format_response(obj, obj.name, highlight)

@category.command
def all() -> None:
    ''' Retrieve all categories '''
    service = client.financial_categories.all

    if response := _handle_response(service):
        for obj in response.results:
            highlight = f'{obj.parent.name}' if obj.parent.name else ''
            remove = ['name', 'is_active', 'parent']
            _format_response(obj, obj.name, highlight, remove=remove)

@category.command
@click.argument('id')
@click.option('--name', help='Inform the new name for the Category.')
@click.option('--parent', help='Inform the Parent Caregory if any.')
@click.option('--activate', is_flag=True, help='Activate category.')
def update(id: str, name: str, parent: str, activate: bool) -> None:
    ''' Update a category '''
    data: dict[str, Any] = {}
    data.update({'name': name}) if name else None
    data.update({'parent': parent}) if parent else None
    data.update({'is_active': True}) if activate else None

    service = client.financial_categories.update

    if obj := _handle_response(service, data, id):
        highlight = f'{obj.parent.name}' if obj.parent.name else ''
        _format_response(obj, 'UPDATED', highlight, color='green')

@category.command
@click.argument('id')
def delete(id: str) -> None:
    ''' Delete a category '''
    service = client.financial_categories.delete

    while True:
        prompt = click.style('Proceed to deletion? [y/n] ', fg='red')
        confirmation = input(prompt)
        if confirmation == 'n':
            sys.exit(0)
        elif confirmation == 'y':
            break

    if obj := _handle_response(service, resource_id=id):
        _format_response(obj, 'DEACTIVATED', 'Done', color='red')
