import math

from rest_framework import serializers

from apps.common.serializer_helpers import ammo_detail, batch_detail
# Intentional cross-app import: tools depends on gears for ammo linking.
from apps.gears.models import Ammo, GearStatus, ReloadedAmmoBatch

from .models import ChronographAnalysis, Shot, ShotGroup


# ── Stats helper ──────────────────────────────────────────────────────────────

def _compute_stats(shot_qs):
    """
    Compute velocity statistics from a Shot queryset.

    Uses population standard deviation (divide by N) — consistent with how
    chronograph software typically reports SD for a complete string of fire.
    Returns None for all values when there are no shots.
    """
    fps_values = [float(s.velocity_fps) for s in shot_qs]
    count = len(fps_values)
    if count == 0:
        return {
            'count': 0,
            'avg_fps': None, 'avg_mps': None,
            'sd_fps': None,
            'es_fps': None,
            'min_fps': None, 'max_fps': None,
        }
    avg = sum(fps_values) / count
    min_fps = min(fps_values)
    max_fps = max(fps_values)
    variance = sum((v - avg) ** 2 for v in fps_values) / count
    return {
        'count': count,
        'avg_fps': round(avg, 1),
        'avg_mps': round(avg * 0.3048, 1),
        'sd_fps': round(math.sqrt(variance), 1),
        'es_fps': round(max_fps - min_fps, 1),
        'min_fps': round(min_fps, 1),
        'max_fps': round(max_fps, 1),
    }


# ── Shot ──────────────────────────────────────────────────────────────────────

class ShotSerializer(serializers.ModelSerializer):
    class Meta:
        model = Shot
        fields = ['id', 'shot_number', 'velocity_fps', 'notes']
        read_only_fields = ['shot_number']

    def validate(self, attrs):
        instance = Shot(**attrs)
        instance.clean()
        return attrs


# ── ShotGroup (nested under analysis) ────────────────────────────────────────

class ShotGroupSerializer(serializers.ModelSerializer):
    # Write: accept a ReloadedAmmoBatch PK (nullable)
    ammo_batch = serializers.PrimaryKeyRelatedField(
        queryset=ReloadedAmmoBatch.objects.all(),
        required=False, allow_null=True,
    )
    ammo = serializers.PrimaryKeyRelatedField(
        queryset=Ammo.objects.filter(status=GearStatus.VERIFIED),
        required=False, allow_null=True,
    )
    # Read: compact inline summaries
    ammo_batch_detail = serializers.SerializerMethodField()
    ammo_detail = serializers.SerializerMethodField()
    shots = ShotSerializer(many=True, read_only=True)
    stats = serializers.SerializerMethodField()

    class Meta:
        model = ShotGroup
        fields = [
            'id', 'label', 'distance_m', 'order', 'notes',
            'ammo_batch',        # write / read (PK)
            'ammo_batch_detail', # read (inline)
            'ammo',              # write / read (PK)
            'ammo_detail',       # read (inline)
            'shots',
            'stats',
        ]

    def get_stats(self, obj):
        return _compute_stats(obj.shots.all())

    def get_ammo_batch_detail(self, obj):
        if not obj.ammo_batch_id:
            return None
        return batch_detail(obj.ammo_batch)

    def get_ammo_detail(self, obj):
        if not obj.ammo_id:
            return None
        return ammo_detail(obj.ammo)

    def validate(self, attrs):
        check_attrs = {k: v for k, v in attrs.items() if k not in ('ammo_batch', 'ammo')}
        instance = ShotGroup(**check_attrs)
        instance.clean()
        return attrs

    def create(self, validated_data):
        analysis = self.context.get('analysis')
        if analysis is not None:
            validated_data['analysis'] = analysis
        return ShotGroup.objects.create(**validated_data)


# ── ShotGroup (standalone, top-level /api/groups/) ───────────────────────────

class ShotGroupStandaloneSerializer(ShotGroupSerializer):
    """
    Extends ShotGroupSerializer with writable `analysis` field for
    standalone groups or groups linked to an analysis after creation.
    """
    analysis = serializers.PrimaryKeyRelatedField(
        queryset=ChronographAnalysis.objects.none(),  # narrowed in __init__
        required=False, allow_null=True,
    )
    ammo_batch = serializers.PrimaryKeyRelatedField(
        queryset=ReloadedAmmoBatch.objects.none(),    # narrowed in __init__
        required=False, allow_null=True,
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        request = self.context.get('request')
        if request and request.user.is_authenticated:
            self.fields['analysis'].queryset = ChronographAnalysis.objects.filter(
                user=request.user
            )
            self.fields['ammo_batch'].queryset = ReloadedAmmoBatch.objects.filter(
                recipe__user=request.user
            )

    class Meta(ShotGroupSerializer.Meta):
        fields = ShotGroupSerializer.Meta.fields + ['analysis']

    def create(self, validated_data):
        # user is injected by perform_create
        return ShotGroup.objects.create(**validated_data)


# ── ChronographAnalysis ───────────────────────────────────────────────────────

class ChronographAnalysisListSerializer(serializers.ModelSerializer):
    class Meta:
        model = ChronographAnalysis
        fields = ['id', 'user', 'name', 'date', 'is_public', 'created_at']
        read_only_fields = ['user', 'created_at']


class ChronographAnalysisDetailSerializer(serializers.ModelSerializer):
    shot_groups = ShotGroupSerializer(many=True, read_only=True)

    class Meta:
        model = ChronographAnalysis
        fields = [
            'id', 'user', 'name', 'date', 'notes', 'is_public',
            'shot_groups',
            'created_at', 'updated_at',
        ]
        read_only_fields = ['user', 'created_at', 'updated_at']

    def validate(self, attrs):
        # For partial updates, fall back to the existing instance's name
        if self.instance is not None:
            name = attrs.get('name', self.instance.name)
        else:
            name = attrs.get('name')
        if not name or not str(name).strip():
            raise serializers.ValidationError({'name': 'Name may not be blank.'})
        return attrs
