import 'package:flutter/material.dart';

import '../../../../common/extension/extension.dart';
import '../../../../common/theme/theme.dart';
import '../../../../domain/order/order.dart';
import '../../../components/spaces/space.dart';

class OrderInformation extends StatelessWidget {
  final Order? order;
  const OrderInformation({super.key, this.order});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: AppColor.primary,
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                order?.orderNumber ?? "",
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
              _buildStatus(),
            ],
          ),
          const SizedBox(height: 8),
          Row(
            children: [
              if (order?.orderType == 'dineIn') ...[
                _buildRowItem(
                  Icons.table_restaurant_outlined,
                  'Meja ${order?.tableNumber}',
                ),
                const SizedBox(width: 16),
              ],
              _buildRowItem(Icons.restaurant_outlined, '${order?.orderType}'),
            ],
          ),
          const SizedBox(height: 8),
          Text(
            'Pelanggan: ${order?.metadata['customer_name'] ?? ""}',
            style: const TextStyle(color: Colors.white, fontSize: 14),
          ),
          Text(
            'Dibuat: ${order?.createdAt.toFormattedDateTime() ?? ""}',
            style: const TextStyle(color: Colors.white70, fontSize: 12),
          ),
        ],
      ),
    );
  }

  Row _buildRowItem(IconData icon, String title) {
    return Row(
      children: [
        Icon(icon, color: Colors.white70, size: 16),
        const SpaceWidth(4),
        Text(
          title,
          style: const TextStyle(color: Colors.white70, fontSize: 14),
        ),
      ],
    );
  }

  Container _buildStatus() {
    switch (order?.status) {
      case 'pending':
        return Container(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
          decoration: BoxDecoration(
            color: Colors.orange,
            borderRadius: BorderRadius.circular(12),
          ),
          child: Text(
            (order?.status ?? "").toUpperCase(),
            style: const TextStyle(
              color: Colors.white,
              fontSize: 12,
              fontWeight: FontWeight.bold,
            ),
          ),
        );
      case 'completed':
        return Container(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
          decoration: BoxDecoration(
            color: Colors.greenAccent,
            borderRadius: BorderRadius.circular(12),
          ),
          child: Text(
            (order?.status ?? "").toUpperCase(),
            style: const TextStyle(
              color: Colors.white,
              fontSize: 12,
              fontWeight: FontWeight.bold,
            ),
          ),
        );
      default:
        return Container(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
          decoration: BoxDecoration(
            color: Colors.grey,
            borderRadius: BorderRadius.circular(12),
          ),
          child: Text(
            (order?.status ?? "").toUpperCase(),
            style: const TextStyle(
              color: Colors.white,
              fontSize: 12,
              fontWeight: FontWeight.bold,
            ),
          ),
        );
    }
  }
}
