"""Check every source byte on every probe, using parallel SHA-256 chunk digests.

The initial/full source verification still compares the original complete-file
SHA-256. The warm token is a length-delimited ordered list of SHA-256 chunks,
not a timestamp cache or sampled fingerprint. Human revisions are never cached.
"""
from concurrent.futures import ThreadPoolExecutor,wait
import errno
import hashlib
import os
import stat
import time

from .mask_taxonomy_source_guard import SourceGuard
from .pallet_description_round4_guard import ContentSourceGuard,binding_guard as content_guard

CHUNK_SIZE=4*1024*1024


def chunk_digest(task):
    fd,offset,size=task;value=os.pread(fd,size,offset)
    if len(value)!=size:raise OSError(errno.EIO,'Source length changed during content probe')
    return hashlib.sha256(value).digest()


class ParallelContentSourceGuard(ContentSourceGuard):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        self.pool=ThreadPoolExecutor(max_workers=min(8,max(1,len(os.sched_getaffinity(0)))),thread_name_prefix='source-content')
        self.content_hash_passes=0;self.content_hash_seconds=0.;self.content_bytes_read=0

    def _fingerprints(self):
        started=time.perf_counter();fds=[];futures=[];layout=[]
        try:
            tasks=[]
            for name in sorted(self.expected_files):
                fd=os.open(self.project/name,os.O_RDONLY|os.O_CLOEXEC|os.O_NONBLOCK);fds.append(fd);info=os.fstat(fd)
                if not stat.S_ISREG(info.st_mode):raise OSError(errno.EINVAL,'Source is not a regular file')
                first=len(tasks)
                for offset in range(0,info.st_size,CHUNK_SIZE):tasks.append((fd,offset,min(CHUNK_SIZE,info.st_size-offset)))
                layout.append((name,info.st_size,first,len(tasks)))
                self.content_bytes_read+=info.st_size
            futures=[self.pool.submit(chunk_digest,task) for task in tasks]
            values=[future.result() for future in futures]
            return tuple((name,size,tuple(values[first:last])) for name,size,first,last in layout)
        finally:
            # A short read/error must not close an fd while another worker still
            # reads it, or accidentally read an unrelated subsequently reused fd.
            wait(futures)
            for fd in fds:os.close(fd)
            self.content_hash_passes+=1;self.content_hash_seconds+=time.perf_counter()-started

    def _token(self):
        return SourceGuard._token(self),('ordered_sha256_4MiB_chunks_v1',self._fingerprints())

    def stats(self):
        with self.lock:
            return dict(super().stats(),content_hash_passes=self.content_hash_passes,content_hash_seconds=self.content_hash_seconds,
                content_bytes_read=self.content_bytes_read,parallel_full_content=True,content_sampling=False,TTL_seconds=0)

    def close(self):
        with self.lock:
            if not self.closed:self.pool.shutdown(wait=True)
            super().close()


def binding_guard():
    original=content_guard()
    try:
        return ParallelContentSourceGuard(project=original.project,databases=original.databases,files=original.files,inventories=original.inventories,
            discover_databases=original.discover_databases,discover_inventories=original.discover_inventories,
            extra_databases=set(original.databases)-original.expected_databases,pinned_files=original.pinned_files)
    finally:original.close()
