from .database import ChangeSet, read_all
from .batch_exe import execute_batch_command

# TODO: merge to batch_api

def clean_scada_device_cs(name: str) -> ChangeSet:
    cs = ChangeSet()

    rows = read_all(name, 'select id from scada_device acs')
    for row in rows:
        cs.delete({ 'type': 'scada_device', 'id': row['id'] })

    return cs


def clean_scada_device_data_cs(name: str) -> ChangeSet:
    cs = ChangeSet()

    rows = read_all(name, 'select distinct device_id from scada_device_data acs')
    for row in rows:
        cs.update({ 'type': 'scada_device_data', 'device_id': row['device_id'], 'data': [] })

    return cs


def clean_scada_element_cs(name: str) -> ChangeSet:
    cs = ChangeSet()

    rows = read_all(name, 'select id from scada_element acs')
    for row in rows:
        cs.delete({ 'type': 'scada_element', 'id': row['id'] })

    return cs


def clean_scada_device(name: str) -> ChangeSet:
    return execute_batch_command(name, clean_scada_device_cs(name))


def clean_scada_device_data(name: str) -> ChangeSet:
    return execute_batch_command(name, clean_scada_device_data_cs(name))


def clean_scada_element(name: str) -> ChangeSet:
    return execute_batch_command(name, clean_scada_element_cs(name))
