from .base import (
    AssetSchema,
    CERBERUS_RULE_OPTIONAL_STRING,
    CERBERUS_RULE_REQUIRED_NONEMPTY_STRING,
    ImageSchema,
)

GENRE_UUIDS = [
    '06368e95-439d-3ef9-a664-db69455d3a64',
    'f736bd52-dedd-3d41-8b48-918c54dad12b'
]
SERIES_UUIDS = [
    'ed7a0a3d-74e8-3237-8454-c3862c0f8838'
]


class MagazineIssueSchema(AssetSchema):
    def __init__(self):
        super(MagazineIssueSchema, self).__init__()
        self['content_type']['allowed'] = ['magazine:issue']
        self['magazine_id'] = CERBERUS_RULE_REQUIRED_NONEMPTY_STRING
        self['year'] = CERBERUS_RULE_REQUIRED_NONEMPTY_STRING
        self['issue_number'] = CERBERUS_RULE_REQUIRED_NONEMPTY_STRING
        self['issue_label'] = CERBERUS_RULE_REQUIRED_NONEMPTY_STRING
        self['cover_image'] = {
            'type': 'dict',
            'required': True,
            'schema': ImageSchema()
        }
        self['promo_image'] = {
            'type': 'dict',
            'required': False,
            'schema': ImageSchema()
        }
        self['stories'] = {
            'type': 'list',
            'required': True,
            'schema': {
                'type': 'dict',
                'schema': {
                    'order': {'type': 'integer'},
                    'article_id': CERBERUS_RULE_REQUIRED_NONEMPTY_STRING,
                    'genre': {
                        'type': 'dict',
                        'required': True,
                        'schema': {
                            'name': CERBERUS_RULE_OPTIONAL_STRING,
                            'uuid': {
                                'type': 'uuid',
                                'required': True,
                                'empty': False,
                                'allowed': GENRE_UUIDS
                            }
                        }
                    },
                    'series': {
                        'type': 'dict',
                        'required': False,
                        'default': {},
                        'anyof': [{'maxlength': 0}, {'schema': {
                            'name': CERBERUS_RULE_OPTIONAL_STRING,
                            'uuid': {
                                'type': 'uuid',
                                'required': True,
                                'empty': False,
                                'allowed': SERIES_UUIDS
                            }
                        }}]
                    }
                }
            }
        }
