import 'package:flutter/material.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

class StockLevelIndicator extends StatelessWidget {
  final int current;
  final int maximum;
  final int? minimum;
  final bool showLabels;
  
  const StockLevelIndicator({
    super.key,
    required this.current,
    required this.maximum,
    this.minimum,
    this.showLabels = false,
  });
  
  Color _getColor(double percentage) {
    if (percentage <= 0.2) {
      return Colors.red;
    } else if (percentage <= 0.4) {
      return Colors.orange;
    } else if (percentage <= 0.6) {
      return Colors.yellow[700]!;
    } else if (percentage <= 0.8) {
      return Colors.blue;
    } else {
      return Colors.green;
    }
  }
  
  String _getLabel(double percentage) {
    if (percentage <= 0.2) {
      return '매우 낮음';
    } else if (percentage <= 0.4) {
      return '낮음';
    } else if (percentage <= 0.6) {
      return '보통';
    } else if (percentage <= 0.8) {
      return '충분';
    } else {
      return '매우 충분';
    }
  }
  
  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    final percentage = maximum > 0 ? (current / maximum).clamp(0.0, 1.0) : 0.0;
    final color = _getColor(percentage);
    
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (showLabels)
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                _getLabel(percentage),
                style: theme.textTheme.small.copyWith(
                  color: color,
                  fontWeight: FontWeight.w600,
                ),
              ),
              Text(
                '${(percentage * 100).toStringAsFixed(0)}%',
                style: theme.textTheme.small.copyWith(
                  color: theme.colorScheme.mutedForeground,
                ),
              ),
            ],
          ),
        if (showLabels) const SizedBox(height: 4),
        Container(
          height: 8,
          decoration: BoxDecoration(
            color: theme.colorScheme.muted,
            borderRadius: BorderRadius.circular(4),
          ),
          child: Stack(
            children: [
              // 최소 재고 표시
              if (minimum != null && maximum > 0)
                Positioned(
                  left: (minimum! / maximum * MediaQuery.of(context).size.width).clamp(0.0, double.infinity),
                  top: 0,
                  bottom: 0,
                  child: Container(
                    width: 2,
                    color: Colors.grey[600],
                  ),
                ),
              
              // 현재 재고 표시
              AnimatedContainer(
                duration: const Duration(milliseconds: 300),
                width: percentage * MediaQuery.of(context).size.width,
                decoration: BoxDecoration(
                  color: color,
                  borderRadius: BorderRadius.circular(4),
                ),
              ),
            ],
          ),
        ),
        if (showLabels && minimum != null) ...[
          const SizedBox(height: 4),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '최소: $minimum',
                style: theme.textTheme.small.copyWith(
                  color: theme.colorScheme.mutedForeground,
                  fontSize: 10,
                ),
              ),
              Text(
                '현재: $current',
                style: theme.textTheme.small.copyWith(
                  color: theme.colorScheme.foreground,
                  fontSize: 10,
                  fontWeight: FontWeight.w600,
                ),
              ),
              Text(
                '최대: $maximum',
                style: theme.textTheme.small.copyWith(
                  color: theme.colorScheme.mutedForeground,
                  fontSize: 10,
                ),
              ),
            ],
          ),
        ],
      ],
    );
  }
}