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

from domainmaster.commands.base import BaseCommand
from domainmaster.errors import DomainmasterError
from domainmaster.models.domain import Domain


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

    @dataclass
    class ParamsEU:
        domain: str
        contact_registrant: str
        contact_tech: List[str]
        idacc: str
        iddealer: Optional[int] = None
        period: int = 1
        nserver: Optional[List[str]] = None
        glue: Optional[List[str]] = None

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

        response = self.requestor.request(payload)

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

        response = Domain(
            domain=response['domain'],
            registrant=response['registrant'],
            tech=response['tech'],
            nserver=response['nserver'],
            glue=response['glue'],
            create_date=response['create_date'],
            expiry_date=response['expiry_date'],
            idacc=response['idacc'],
            iddealer=response['iddealer']
        )
        return response
