import concurrent.futures
import http.client
import http.server
import json
import os
import socket
import tempfile
import threading
import time
import unittest
from pathlib import Path
from unittest.mock import patch

import ornithctl as ctl
from ornith_gateway import Admission, UnixControl, control_handler, handler_class


def profile(name, status='validated'):
    return {'id': name, 'status': status, 'reason': 'test-only evidence',
            'command': ['/bin/bash', '/test/' + name], 'env': {},
            'model_id': 'fixture', 'context': 128, 'concurrency': 1,
            'evidence': ['fixture validation']}


class Services:
    def __init__(self, active=True):
        self.active = active
        self.calls = []
        self.foreign = False

    def check_safety(self, profile=None):
        pass

    def owned(self, gateway=False):
        if self.foreign:
            raise ctl.Error('Foreign unit')
        return True if gateway else self.active

    def info(self, gateway=False):
        return {'ActiveState': 'active' if self.active else 'inactive'}

    def stop(self):
        self.owned()
        self.calls.append('stop')
        self.active = False

    def start(self, item):
        self.check_safety(item)
        self.calls.append('start:' + item['id'])
        self.active = True

    def ensure_gateway(self):
        self.calls.append('gateway')


class Gate:
    def __init__(self):
        self.draining = False
        self.allow_drain = True
        self.calls = []

    def call(self, command):
        return {'active': 0, 'draining': self.draining}

    def drain(self, timeout):
        self.calls.append('drain')
        self.draining = True
        return self.allow_drain

    def resume(self):
        self.calls.append('resume')
        self.draining = False


class ControllerTests(unittest.TestCase):
    def setUp(self):
        self.tmp = tempfile.TemporaryDirectory()
        self.root = Path(self.tmp.name)
        (self.root / 'profiles').mkdir()
        (self.root / 'state').mkdir()
        self.a, self.b = profile('baseline'), profile('candidate')
        registry = {'engine': 'vllm', 'endpoint': 'http://127.0.0.1:8140',
                    'gpu_uuid': 'GPU-test', 'baseline_id': 'baseline',
                    'profiles': [self.a, self.b, profile('experimental', 'experimental')]}
        (self.root / 'profiles/vllm.json').write_text(json.dumps(registry))
        self.state = self.root / 'state/vllm.json'
        ctl.atomic_json(self.state, {'phase': 'ready', 'current': 'baseline',
                                   'current_profile': self.a, 'previous_profile': None})
        self.service, self.gate = Services(), Gate()
        self.probed = []

    def tearDown(self):
        self.tmp.cleanup()

    def controller(self, probe=None):
        return ctl.Controller('vllm', self.root, self.service, self.gate,
                              probe or (lambda p: self.probed.append(p['id'])), drain_timeout=.1)

    def test_success_and_explicit_rollback(self):
        self.assertEqual(self.controller().switch('candidate')['result'], 'activated')
        self.assertFalse(self.gate.draining)
        self.assertEqual(ctl.read_json(self.state)['previous'], 'baseline')
        self.controller().switch(rollback=True)
        self.assertEqual(ctl.read_json(self.state)['current'], 'baseline')
        self.assertEqual(self.service.calls, ['stop', 'start:candidate', 'stop', 'start:baseline'])

    def test_failure_automatically_restores_prior_and_admission(self):
        def probe(item):
            if item['id'] == 'candidate':
                raise ctl.Error('fixture generation failure')
        with self.assertRaisesRegex(ctl.Error, 'previous profile restored'):
            self.controller(probe).switch('candidate')
        self.assertEqual(ctl.read_json(self.state)['current'], 'baseline')
        self.assertFalse(self.gate.draining)
        self.assertEqual(self.service.calls, ['stop', 'start:candidate', 'stop', 'start:baseline'])

    def test_rollback_failure_leaves_admission_closed(self):
        def probe(item):
            raise ctl.Error('fixture persistent failure')
        with self.assertRaisesRegex(ctl.Error, 'recovery failed'):
            self.controller(probe).switch('candidate')
        self.assertTrue(self.gate.draining)
        self.assertEqual(ctl.read_json(self.state)['phase'], 'recovery-required')

    def test_drain_timeout_does_not_stop_old_worker(self):
        self.gate.allow_drain = False
        with self.assertRaisesRegex(ctl.Error, 'Drain timed out'):
            self.controller().switch('candidate')
        self.assertEqual(self.service.calls, [])
        self.assertFalse(self.gate.draining)
        self.assertEqual(ctl.read_json(self.state)['phase'], 'ready')

    def test_thermal_latch_blocks_before_drain_or_stop(self):
        with patch.object(self.service, 'check_safety', side_effect=ctl.Error('Thermal safety latch present')):
            with self.assertRaisesRegex(ctl.Error, 'Thermal safety'):
                self.controller().switch('candidate')
        self.assertEqual(self.gate.calls, [])
        self.assertEqual(self.service.calls, [])

    def test_thermal_stop_during_probe_blocks_automatic_restart(self):
        latched = False
        def safety(profile=None):
            if latched:
                raise ctl.Error('Thermal safety latch present')
        def probe(item):
            nonlocal latched
            latched = True
            raise ctl.Error('fixture thermal stop during candidate probe')
        with patch.object(self.service, 'check_safety', side_effect=safety):
            with self.assertRaisesRegex(ctl.Error, 'recovery failed'):
                self.controller(probe).switch('candidate')
        self.assertEqual(self.service.calls, ['stop', 'start:candidate', 'stop'])
        self.assertTrue(self.gate.draining)
        self.assertEqual(ctl.read_json(self.state)['phase'], 'recovery-required')

    def test_experimental_and_missing_evidence_refused_before_drain(self):
        with self.assertRaisesRegex(ctl.Error, 'experimental'):
            self.controller().switch('experimental')
        controller = self.controller()
        controller.profiles['candidate']['evidence'] = []
        with self.assertRaisesRegex(ctl.Error, 'no evidence'):
            controller.switch('candidate')
        self.assertEqual(self.gate.calls, [])
        self.assertEqual(self.service.calls, [])

    def test_foreign_unit_never_stopped(self):
        self.service.foreign = True
        with self.assertRaisesRegex(ctl.Error, 'Foreign'):
            self.controller().switch('candidate')
        self.assertEqual(self.service.calls, [])
        self.assertEqual(self.gate.calls, [])

    def test_untracked_worker_refused(self):
        self.state.unlink()
        with self.assertRaisesRegex(ctl.Error, 'lacks saved'):
            self.controller().switch('candidate')
        self.assertEqual(self.service.calls, [])

    def test_read_status_does_not_mutate(self):
        before = self.state.read_bytes()
        self.controller().status()
        self.assertEqual(before, self.state.read_bytes())
        self.assertEqual(self.service.calls, [])
        self.assertEqual(self.gate.calls, [])

    def test_list_reason_and_status_do_not_reveal_environment(self):
        controller = self.controller()
        controller.profiles['candidate']['env'] = {'FIXTURE_PRIVATE_TOKEN': 'do-not-display-fixture'}
        state = ctl.read_json(self.state)
        state['current_profile']['env'] = {'FIXTURE_PRIVATE_TOKEN': 'do-not-display-fixture'}
        ctl.atomic_json(self.state, state)
        output = json.dumps([controller.listing(), controller.status()])
        self.assertIn('test-only evidence', output)
        self.assertNotIn('FIXTURE_PRIVATE_TOKEN', output)
        self.assertNotIn('do-not-display-fixture', output)

    def test_profile_arguments_and_health_timeout_schema(self):
        item = profile('argument')
        item['command'].append('awq-bf16-eager')
        item['health_timeout'] = 900
        ctl.validate_profile(item)
        item['health_timeout'] = 0
        with self.assertRaisesRegex(ctl.Error, 'health_timeout'):
            ctl.validate_profile(item)

    def test_interrupted_transaction_refused(self):
        ctl.atomic_json(self.state, {'phase': 'changing'})
        with self.assertRaisesRegex(ctl.Error, 'operator recovery'):
            self.controller().switch('candidate')
        self.assertEqual(self.service.calls, [])

    def test_initial_activation(self):
        self.state.unlink()
        self.service.active = False
        self.controller().switch('baseline')
        self.assertEqual(self.service.calls, ['start:baseline', 'gateway'])
        self.assertFalse(self.gate.draining)

    def test_persistence_failure_after_drain_resumes_unchanged_worker(self):
        with patch.object(ctl, 'atomic_json', side_effect=OSError('fixture disk full')):
            with self.assertRaisesRegex(ctl.Error, 'worker unchanged'):
                self.controller().switch('candidate')
        self.assertEqual(self.service.calls, [])
        self.assertEqual(self.gate.calls, ['drain', 'resume'])
        self.assertFalse(self.gate.draining)
        self.assertEqual(ctl.read_json(self.state)['current'], 'baseline')

    def test_already_current_checks_worker_and_probe(self):
        self.assertEqual(self.controller().switch('baseline')['result'], 'already-current')
        self.assertEqual(self.probed, ['baseline'])
        self.assertEqual(self.service.calls, [])
        self.service.active = False
        with self.assertRaisesRegex(ctl.Error, 'not active'):
            self.controller().switch('baseline')

    def test_already_current_refuses_missing_gateway(self):
        with patch.object(self.service, 'owned', side_effect=lambda gateway=False: not gateway):
            with self.assertRaisesRegex(ctl.Error, 'gateway is not active'):
                self.controller().switch('baseline')
        self.assertEqual(self.service.calls, [])

    def test_already_current_refuses_draining_gateway(self):
        self.gate.draining = True
        with self.assertRaisesRegex(ctl.Error, 'is draining'):
            self.controller().switch('baseline')
        self.assertEqual(self.service.calls, [])
        self.assertTrue(self.gate.draining)

    def test_already_current_propagates_probe_failure_without_restart(self):
        def probe(item):
            raise ctl.Error('fixture unhealthy')
        with self.assertRaisesRegex(ctl.Error, 'fixture unhealthy'):
            self.controller(probe).switch('baseline')
        self.assertEqual(self.service.calls, [])

    def test_lock_prevents_concurrent_changes(self):
        with open(self.state.with_suffix('.lock'), 'a') as lock:
            ctl.fcntl.flock(lock, ctl.fcntl.LOCK_EX | ctl.fcntl.LOCK_NB)
            with self.assertRaisesRegex(ctl.Error, 'already in progress'):
                self.controller().switch('candidate')


class OwnershipTests(unittest.TestCase):
    def test_power_limit_is_checked_read_only(self):
        with tempfile.TemporaryDirectory() as directory:
            service = ctl.Systemd('vllm', 'GPU-fixture', Path(directory))
            item = dict(profile('fixture'), max_power_limit_w=250)
            with patch('subprocess.check_output', return_value='300.00\n') as query:
                with self.assertRaisesRegex(ctl.Error, 'requires at most 250'):
                    service.check_safety(item)
                self.assertEqual(query.call_args.args[0][2], 'GPU-fixture')
                self.assertIn('--query-gpu=power.limit', query.call_args.args[0])
            with patch('subprocess.check_output', return_value='250.00\n'):
                service.check_safety(item)
            with patch('subprocess.check_output', return_value='N/A\n'):
                with self.assertRaisesRegex(ctl.Error, 'Cannot verify'):
                    service.check_safety(item)

    def test_thermal_latch_prevents_worker_start(self):
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            (root / 'vllm').mkdir()
            (root / 'vllm/thermal-stop.json').write_text('{}')
            service = ctl.Systemd('vllm', 'GPU-test', root)
            with patch.object(service, 'run_unit') as run:
                with self.assertRaisesRegex(ctl.Error, 'Thermal safety'):
                    service.start(profile('fixture'))
                run.assert_not_called()

    def test_exec_environment_is_explicit_not_inherited(self):
        service = ctl.Systemd('vllm', 'GPU-test')
        with patch.object(service, 'owned', return_value=False), patch.dict(os.environ, {'FIXTURE_PRIVATE_TOKEN': 'must-not-inherit'}), patch('subprocess.run') as run:
            run.return_value.returncode = 0
            service.run_unit(['/bin/bash', '/fixture.sh'], {'CUDA_VISIBLE_DEVICES': 'GPU-test'})
            args = run.call_args.args[0]
            index = args.index('/usr/bin/env')
            self.assertEqual(args[index + 1], '-i')
            self.assertIn('CUDA_VISIBLE_DEVICES=GPU-test', args)
            self.assertNotIn('must-not-inherit', ' '.join(args))
            self.assertNotIn('FIXTURE_PRIVATE_TOKEN', ' '.join(args))

    def test_foreign_service_never_invokes_stop(self):
        service = ctl.Systemd('vllm', 'GPU-test')
        with patch.object(service, 'info', return_value={'LoadState': 'loaded', 'Transient': 'no', 'Description': 'foreign'}), patch('subprocess.run') as run:
            with self.assertRaisesRegex(ctl.Error, 'unowned'):
                service.stop()
            run.assert_not_called()

    def test_foreign_backend_port_never_adopted(self):
        with tempfile.TemporaryDirectory() as path, socket.socket() as listener:
            launcher = Path(path) / 'launch.sh'
            launcher.write_text('exit 0\n')
            listener.bind(('127.0.0.1', 0))
            listener.listen()
            item = profile('fixture')
            item['command'][1] = str(launcher)
            service = ctl.Systemd('vllm', 'GPU-test', Path(path))
            with patch.dict(ctl.PORTS, {'vllm': (0, listener.getsockname()[1])}), patch('subprocess.run') as run:
                with self.assertRaisesRegex(ctl.Error, 'unidentified'):
                    service.start(item)
                run.assert_not_called()

    def test_initial_public_listener_prevents_new_gpu_worker(self):
        with tempfile.TemporaryDirectory() as path, socket.socket() as listener:
            launcher = Path(path) / 'launch.sh'
            launcher.write_text('exit 0\n')
            listener.bind(('127.0.0.1', 0))
            listener.listen()
            item = profile('fixture')
            item['command'][1] = str(launcher)
            service = ctl.Systemd('vllm', 'GPU-test', Path(path))
            with patch.dict(ctl.PORTS, {'vllm': (listener.getsockname()[1], 0)}), patch.object(service, 'owned', return_value=False), patch('subprocess.run') as run:
                with self.assertRaisesRegex(ctl.Error, 'unmanaged server'):
                    service.start(item)
                run.assert_not_called()


class ProxyTests(unittest.TestCase):
    def setUp(self):
        self.release = threading.Event()
        self.started = threading.Event()
        release, started = self.release, self.started
        class Backend(http.server.BaseHTTPRequestHandler):
            protocol_version = 'HTTP/1.1'
            def log_message(self, *args):
                pass
            def do_GET(self):
                self.send_response(200)
                self.send_header('Content-Type', 'text/event-stream')
                self.send_header('Transfer-Encoding', 'chunked')
                self.end_headers()
                first = b'data: first\n\n'
                self.wfile.write(b'%x\r\n' % len(first) + first + b'\r\n')
                self.wfile.flush()
                started.set()
                release.wait(5)
                last = b'data: [DONE]\n\n'
                self.wfile.write(b'%x\r\n' % len(last) + last + b'\r\n0\r\n\r\n')
                self.wfile.flush()
            def do_POST(self):
                data = self.rfile.read(int(self.headers['Content-Length']))
                self.send_response(201)
                self.send_header('Content-Type', 'application/json')
                self.send_header('Content-Length', str(len(data)))
                self.end_headers()
                self.wfile.write(data)
        self.backend = http.server.ThreadingHTTPServer(('127.0.0.1', 0), Backend)
        self.admission = Admission()
        self.proxy = http.server.ThreadingHTTPServer(('127.0.0.1', 0), handler_class(self.admission, self.backend.server_port))
        self.tmp = tempfile.TemporaryDirectory()
        self.socket = Path(self.tmp.name) / 'control.sock'
        self.control = UnixControl(str(self.socket), control_handler(self.admission))
        os.chmod(self.socket, 0o600)
        self.gate = ctl.Gateway(self.socket)
        for server in [self.backend, self.proxy, self.control]:
            threading.Thread(target=server.serve_forever, daemon=True).start()

    def tearDown(self):
        self.release.set()
        for server in [self.backend, self.proxy, self.control]:
            server.shutdown()
            server.server_close()
        self.tmp.cleanup()

    def request(self, method='GET', body=None):
        conn = http.client.HTTPConnection('127.0.0.1', self.proxy.server_port, timeout=5)
        conn.request(method, '/v1/chat/completions', body=body)
        return conn, conn.getresponse()

    def test_stream_forwarding_drain_wait_and_resume(self):
        conn, response = self.request()
        # First SSE event arrives while the backend is deliberately unfinished.
        self.assertEqual(response.read(len(b'data: first\n\n')), b'data: first\n\n')
        self.assertFalse(self.release.is_set())
        self.assertEqual(self.gate.call('status')['active'], 1)
        with concurrent.futures.ThreadPoolExecutor() as pool:
            draining = pool.submit(self.gate.drain, 3)
            for _ in range(100):
                if self.gate.call('status')['draining']:
                    break
                time.sleep(.01)
            rejected, failure = self.request()
            self.assertEqual(failure.status, 503)
            failure.read()
            rejected.close()
            self.assertFalse(draining.done())
            self.release.set()
            self.assertEqual(response.read(), b'data: [DONE]\n\n')
            self.assertTrue(draining.result(timeout=4))
        conn.close()
        self.gate.resume()
        resumed, answer = self.request('POST', b'{"fixture":true}')
        self.assertEqual(answer.status, 201)
        self.assertEqual(answer.read(), b'{"fixture":true}')
        resumed.close()

    def test_actual_drain_timeout_preserves_stream(self):
        conn, response = self.request()
        response.read(len(b'data: first\n\n'))
        self.assertFalse(self.gate.drain(.05))
        self.gate.resume()
        self.release.set()
        self.assertIn(b'[DONE]', response.read())
        conn.close()

    def test_chunked_request_body(self):
        conn = http.client.HTTPConnection('127.0.0.1', self.proxy.server_port)
        conn.request('POST', '/echo', body=iter([b'{"value":', b'42}']), encode_chunked=True)
        response = conn.getresponse()
        self.assertEqual(response.read(), b'{"value":42}')
        conn.close()


if __name__ == '__main__':
    unittest.main(verbosity=2)
