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

import '../models/inbound_record.dart';

/// 입고 트랜잭션 상세 정보를 다이얼로그 본문에 표시하는 뷰이다.
class InboundDetailView extends StatelessWidget {
  const InboundDetailView({
    super.key,
    required this.record,
    required this.currencyFormatter,
    this.transitionsEnabled = true,
  });

  final InboundRecord record;
  final intl.NumberFormat currencyFormatter;
  final bool transitionsEnabled;

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (!transitionsEnabled) ...[
          ShadBadge.outline(
            child: Text('재고 상태 전이가 비활성화된 상태입니다.', style: theme.textTheme.small),
          ),
          const SizedBox(height: 16),
        ],
        Text('라인 품목', style: theme.textTheme.h4),
        const SizedBox(height: 8),
        _buildLineTable(),
      ],
    );
  }

  Widget _buildLineTable() {
    final rowHeight = (record.items.length * 52).clamp(160, 260).toDouble();
    return SizedBox(
      height: rowHeight,
      child: ShadTable.list(
        header: const [
          ShadTableCell.header(child: Text('제품')),
          ShadTableCell.header(child: Text('제조사')),
          ShadTableCell.header(child: Text('단위')),
          ShadTableCell.header(child: Text('수량')),
          ShadTableCell.header(child: Text('단가')),
          ShadTableCell.header(child: Text('비고')),
        ],
        children: [
          for (final item in record.items)
            [
              ShadTableCell(child: Text(item.product)),
              ShadTableCell(child: Text(item.manufacturer)),
              ShadTableCell(child: Text(item.unit)),
              ShadTableCell(child: Text('${item.quantity}')),
              ShadTableCell(child: Text(currencyFormatter.format(item.price))),
              ShadTableCell(
                child: Text(item.remark.isEmpty ? '-' : item.remark),
              ),
            ],
        ],
        columnSpanExtent: (index) => const FixedTableSpanExtent(136),
        rowSpanExtent: (index) => const FixedTableSpanExtent(52),
      ),
    );
  }
}
