from dataclasses import dataclass, asdict
from typing import List

from domainmaster.commands.base import BaseCommand
from domainmaster.errors import DomainmasterError


@dataclass
class DomainRecord:
    domanin: str
    idacc: str
    iddealer: str
    expiry_date: str
    auto_period: str


class ListDomains(BaseCommand):
    COMMAND = 'list domains'

    @dataclass
    class Params:
        pass

    def execute(self, params: Params) -> List[DomainRecord]:
        payload = {
            'command': self.COMMAND,
            'params': asdict(params)
        }

        response = self.requestor.request(payload)

        if response['status'] != 'success':
            raise DomainmasterError(message=response['message'])

        result = []

        for domain_name, domain_data in response['data'].items():
            result.append(
                DomainRecord(
                    domanin=domain_name,
                    idacc=domain_data['idacc'],
                    iddealer=domain_data['iddealer'],
                    expiry_date=domain_data['expiry_date'],
                    auto_period=domain_data['auto_period']
                )
            )

        return result
