import io
import json
import os
import re
import time
import urllib.error
import urllib.parse
import urllib.request
import zipfile

from interop_policy import QUARANTINE, TAILNET, ROLES

CLIENT_ID = 'TG2FEugs2S11CNTRL-kx3fW8WXMt11CNTRL'
TAILNET_API = 'https://api.tailscale.com/api/v2/tailnet/-'
RETRYABLE_HTTP = (408, 500, 502, 503, 504)
MAX_RECORD_BYTES = 65536


class NetworkError(ValueError):
    def __init__(self, message, code=None, retryable=False):
        super().__init__(message)
        self.code = code
        self.retryable = retryable


class NoRedirect(urllib.request.HTTPRedirectHandler):
    def redirect_request(self, *args):
        return None


def decode(body):
    try:
        result = json.loads(body)
    except ValueError:
        raise NetworkError('Invalid JSON response') from None
    if not isinstance(result, dict):
        raise NetworkError('Expected a JSON object')
    return result


def github_rate_limit_hint(url, code, headers):
    if code not in (403, 429) or not url.startswith('https://api.github.com/'):
        return ''
    headers = headers or {}
    if headers.get('X-RateLimit-Remaining') == '0':
        return ' (GitHub API rate limit exhausted)'
    if code == 429 or headers.get('Retry-After', '').isdigit():
        return ' (GitHub API throttled the request)'
    return ''


def fetch(url, token=None, data=None, headers=None, allowed_redirect=False, method=None, operation='Network request'):
    options = {'Accept': 'application/json', **(headers or {})}
    if token:
        options['Authorization'] = 'Bearer ' + token
    try:
        with urllib.request.build_opener(NoRedirect()).open(
                urllib.request.Request(url, data=data, headers=options, method=method), timeout=30) as response:
            body = response.read(1048577)
            if len(body) > 1048576:
                raise NetworkError(operation + ': response exceeds limit')
            return body, response.headers
    except urllib.error.HTTPError as error:
        error.close()
        if error.code == 302 and allowed_redirect:
            target = error.headers.get('Location', '')
            parts = urllib.parse.urlsplit(target)
            if (parts.scheme != 'https' or not (parts.hostname or '').endswith('.blob.core.windows.net')
                    or parts.username or parts.password or parts.port not in (None, 443)):
                raise NetworkError('Unexpected artifact download host') from None
            return fetch(target, operation=operation)
        hint = github_rate_limit_hint(url, error.code, error.headers)
        raise NetworkError(operation + ': HTTP ' + str(error.code) + hint, error.code,
                           retryable=error.code in RETRYABLE_HTTP) from None
    except (OSError, urllib.error.URLError):
        raise NetworkError(operation + ': connection failed', retryable=True) from None


def workflow_event():
    env = os.environ
    if (env.get('GITHUB_REPOSITORY_ID') != '266877886' or env.get('GITHUB_EVENT_NAME') != 'workflow_run'
            or env.get('GITHUB_REF') != 'refs/heads/master'):
        raise NetworkError('Unexpected CI context')
    with open(env['GITHUB_EVENT_PATH'], 'rb') as stream:
        data = stream.read(2097153)
    if len(data) > 2097152:
        raise NetworkError('Workflow event exceeds limit')
    event = decode(data)
    repository = event.get('repository', {})
    source = event.get('workflow_run', {})
    path = event.get('workflow', {}).get('path')
    if (repository.get('id') != 266877886 or repository.get('owner', {}).get('id') != 166180282
            or path not in ('.github/workflows/ci.yml', '.github/workflows/interoperability.yml')
            or type(source.get('id')) is not int or source['id'] <= 0
            or type(source.get('run_attempt')) is not int or source['run_attempt'] <= 0
            or not re.fullmatch(r'[0-9a-f]{40}', source.get('head_sha', ''))):
        raise NetworkError('Unexpected upstream workflow')
    if path == '.github/workflows/ci.yml':
        if (event.get('action') != 'in_progress' or source.get('name') != 'CI'
                or source.get('event') not in ('pull_request', 'push', 'schedule', 'workflow_dispatch')):
            raise NetworkError('Unsupported upstream CI event')
    elif event.get('action') != 'completed' or source.get('event') != 'workflow_run':
        raise NetworkError('Unsupported cleanup event')
    return event


def identity_token():
    workflow_event()
    parts = urllib.parse.urlsplit(os.environ['ACTIONS_ID_TOKEN_REQUEST_URL'])
    if (parts.scheme != 'https' or not (parts.hostname or '').endswith('.actions.githubusercontent.com')
            or parts.username or parts.password or parts.port not in (None, 443)):
        raise NetworkError('Unexpected OIDC endpoint')
    query = [(k, v) for k, v in urllib.parse.parse_qsl(parts.query) if k != 'audience']
    query.append(('audience', 'api.tailscale.com/' + CLIENT_ID))
    url = urllib.parse.urlunsplit(parts._replace(query=urllib.parse.urlencode(query)))
    data, _ = fetch(url, os.environ['ACTIONS_ID_TOKEN_REQUEST_TOKEN'], operation='GitHub OIDC request')
    token = decode(data).get('value')
    if not isinstance(token, str) or not token or len(token) > 32768:
        raise NetworkError('Invalid OIDC response')
    return token


def exchange():
    data = urllib.parse.urlencode({'client_id': CLIENT_ID, 'jwt': identity_token()}).encode()
    body, _ = fetch('https://api.tailscale.com/api/v2/oauth/token-exchange', data=data,
                    headers={'Content-Type': 'application/x-www-form-urlencoded'},
                    operation='Tailscale controller token exchange')
    token = decode(body).get('access_token')
    if not isinstance(token, str) or not token or len(token) > 32768:
        raise NetworkError('Invalid federation response')
    return token


def require_no_oidc():
    if any(os.environ.get(key) for key in ('ACTIONS_ID_TOKEN_REQUEST_URL', 'ACTIONS_ID_TOKEN_REQUEST_TOKEN')):
        raise NetworkError('Worker has OIDC request variables')


def list_artifacts(url, etag):
    body, headers = fetch(url, os.environ['GH_TOKEN'], headers={'If-None-Match': etag} if etag else {},
                          operation='List GitHub artifacts')
    listing = decode(body)
    artifacts = listing['artifacts']
    count = listing['total_count']
    if type(count) is not int or count < 0 or count > 1000:
        raise NetworkError('Artifact listing exceeds rendezvous limit')
    for page in range(2, (count + 99) // 100 + 1):
        body, _ = fetch(url + '&page=' + str(page), os.environ['GH_TOKEN'], operation='List GitHub artifacts')
        artifacts += decode(body)['artifacts']
    return list({item['id']: item for item in artifacts}.values()), headers.get('ETag')


def download_records(run_id, expected, timeout=600, progress=None, *, initial_listing=None):
    if not isinstance(run_id, str) or not re.fullmatch(r'[1-9][0-9]*', run_id):
        raise NetworkError('Invalid artifact source run')
    url = ('https://api.github.com/repos/RsyncProject/rsync/actions/runs/'
           + run_id + '/artifacts?per_page=100')
    deadline = time.monotonic() + timeout
    cached, etag = None, None
    downloaded = {}
    while time.monotonic() < deadline:
        if progress is not None and progress() is True:
            deadline = time.monotonic() + timeout
        try:
            if initial_listing is None:
                cached, etag = list_artifacts(url, etag)
                records = cached
            else:
                records, initial_listing = initial_listing, None
        except NetworkError as error:
            if error.retryable:
                time.sleep(5)
                continue
            if error.code != 304 or cached is None:
                raise
            records = cached
        found = {name: [item for item in records if item['name'] == name and not item['expired']]
                 for name in expected}
        if any(len(items) > 1 for items in found.values()):
            raise NetworkError('Duplicate rendezvous artifact')
        if all(len(items) == 1 for items in found.values()):
            result = {}
            for name, items in found.items():
                artifact_id = items[0]['id']
                if type(artifact_id) is not int or artifact_id <= 0:
                    raise NetworkError('Invalid artifact identity')
                saved = downloaded.get(name)
                if saved is not None and saved[0] == artifact_id:
                    result[expected[name]] = saved[1]
                    continue
                download = 'https://api.github.com/repos/RsyncProject/rsync/actions/artifacts/' + str(artifact_id) + '/zip'
                try:
                    data, _ = fetch(download, os.environ['GH_TOKEN'], allowed_redirect=True, operation='Download GitHub artifact')
                except NetworkError as error:
                    if error.code != 404 and not error.retryable:
                        raise
                    # The listing can precede archive availability, and an
                    # individual archive request can lose its connection.
                    result = None
                    break
                with zipfile.ZipFile(io.BytesIO(data)) as archive:
                    files = [item for item in archive.infolist() if not item.is_dir()]
                    if len(files) != 1 or files[0].file_size > MAX_RECORD_BYTES:
                        raise NetworkError('Invalid rendezvous artifact')
                    raw = archive.read(files[0])
                result[expected[name]] = decode(raw), raw
                downloaded[name] = artifact_id, result[expected[name]]
            if result is not None:
                return result
        time.sleep(5)
    raise NetworkError('Timed out waiting for rendezvous artifacts')


class Tailnet:
    def __init__(self):
        if not CLIENT_ID.startswith(TAILNET + '-'):
            raise NetworkError('Controller credential does not match the configured test namespace')
        self.token = exchange()
        self.base = TAILNET_API

    def devices(self):
        body, _ = fetch(self.base + '/devices', self.token, operation='List Tailscale devices')
        devices = decode(body).get('devices')
        if not isinstance(devices, list) or any(not isinstance(device, dict) for device in devices):
            raise NetworkError('Invalid device list')
        return devices

    def policy(self):
        body, headers = fetch(self.base + '/acl', self.token, operation='Read Tailscale policy')
        return decode(body), headers

    def change(self, transform):
        for _ in range(5):
            policy, headers = self.policy()
            etag = headers.get('ETag')
            if not etag:
                raise NetworkError('Policy response has no concurrency token')
            changed = transform(policy)
            try:
                _, applied = fetch(self.base + '/acl', self.token, json.dumps(changed).encode(),
                                   {'Content-Type': 'application/hujson', 'If-Match': etag}, operation='Update Tailscale policy')
                return dict(before=etag, after=applied.get('ETag'))
            except NetworkError as error:
                if error.code != 412:
                    raise
        raise NetworkError('Policy changed during every admission attempt')

    def delete(self, device_id):
        if not isinstance(device_id, str) or not re.fullmatch(r'[A-Za-z0-9]+', device_id):
            raise NetworkError('Invalid device API identity')
        try:
            fetch('https://api.tailscale.com/api/v2/device/' + device_id, self.token, method='DELETE',
                  operation='Delete Tailscale device')
        except NetworkError as error:
            if error.code != 404:
                raise

    def create_key(self, role, state):
        if role not in ROLES:
            raise NetworkError('Unexpected enrolment role')
        description = 'ci-' + state['run_id'] + '-' + state['run_attempt'] + '-' + role
        payload = dict(expirySeconds=900, description=description,
                       capabilities=dict(devices=dict(create=dict(reusable=False, ephemeral=True, preauthorized=True,
                                                                  tags=[QUARANTINE]))))
        body, _ = fetch(self.base + '/keys', self.token, json.dumps(payload).encode(),
                        {'Content-Type': 'application/json'}, operation='Create quarantine key')
        result = decode(body)
        if (not isinstance(result.get('id'), str) or not re.fullmatch(r'[A-Za-z0-9]+', result['id'])
                or not isinstance(result.get('key'), str) or not result['key'].startswith('tskey-auth-')
                or len(result['key']) > 256):
            raise NetworkError('Invalid quarantine key response')
        return result['id'], result['key']

    def delete_key(self, key_id):
        if not isinstance(key_id, str) or not re.fullmatch(r'[A-Za-z0-9]+', key_id):
            raise NetworkError('Invalid enrolment key identity')
        try:
            fetch(self.base + '/keys/' + key_id, self.token, method='DELETE', operation='Revoke quarantine key')
        except NetworkError as error:
            if error.code != 404:
                raise
