from dataclasses import dataclass, asdict
from typing import List, Optional

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


class RegisterDomainCZ(BaseCommand):
    COMMAND = 'register %s domain'

    @dataclass
    class ParamsCZ:
        domain: str
        registrant: str
        admin: List[str]
        idacc: str
        nsset: Optional[str] = None
        keyset: Optional[str] = None
        iddealer: Optional[int] = None
        period: Optional[int] = None

    def execute(self, domain: str, params):
        payload = {
            'command': self.COMMAND % domain,
            'params': asdict(params)
        }

        response = self.requestor.request(payload)

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

        return response
