import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:tm_app/core/utils/date_utils.dart';
import 'package:tm_app/data/services/model/tender_data/tender_data.dart';

import '../../../core/constants/assets.dart';
import '../../../core/constants/tender_approval_status.dart';
import '../../../core/constants/tender_feedback.dart';
import '../../../core/theme/colors.dart';
import '../../../core/utils/size_config.dart';
import '../../shared/flag.dart';

class TenderCard extends StatelessWidget {
  final bool isDesktop;
  final TenderData tender;
  final String status;
  final String submissionMode;

  const TenderCard({
    required this.isDesktop,
    required this.tender,
    required this.status,
    required this.submissionMode,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    // Determine styling based on tender status
    Color backgroundColor = AppColors.grey0;
    Color borderColor = AppColors.grey30;
    String? statusIcon;
    Color statusCardColor = AppColors.grey0;
    Color statusTextColor = AppColors.grey60;

    if (status == TenderFeedback.liked.value ||
        status == TenderApprovalStatus.submitted.value) {
      backgroundColor = AppColors.green0;
      borderColor = AppColors.green10;
      statusIcon = AssetsManager.tickCircle;
      statusCardColor = AppColors.green20;
      statusTextColor = AppColors.green30;
    }
    if (status == TenderFeedback.disliked.value ||
        status == TenderApprovalStatus.rejected.value) {
      backgroundColor = AppColors.red0;
      borderColor = AppColors.red10;
      statusIcon = AssetsManager.closeCircle;
      statusCardColor = AppColors.red10;
      statusTextColor = AppColors.red20;
    }

    return Container(
      height: isDesktop ? 182.0.h() : 240.0.h(),
      width: double.infinity,
      margin: EdgeInsets.only(
        left: 24.0.w(),
        right: 24.0.w(),
        bottom: 12.0.h(),
      ),
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: borderColor),
      ),
      child: Padding(
        padding: EdgeInsets.all(16.0.w()),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // Top section with date and status
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                // Date
                Text(
                  timeConvertor(int.parse(tender.publicationDate!.toString())),
                  style: TextStyle(
                    color: AppColors.grey60,
                    fontSize: 12.0.sp(),
                    fontWeight: FontWeight.w400,
                  ),
                ),
                // Completed status badge
                Container(
                  padding: EdgeInsets.symmetric(
                    horizontal: 16.0.w(),
                    vertical: 4.0.h(),
                  ),
                  decoration: BoxDecoration(
                    color: statusCardColor,
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Row(
                    children: [
                      if (statusIcon != null)
                        SvgPicture.asset(
                          statusIcon,
                          width: 16.0.w(),
                          height: 16.0.h(),
                          colorFilter: ColorFilter.mode(
                            statusTextColor,
                            BlendMode.srcIn,
                          ),
                        ),
                      SizedBox(width: 4.0.w()),
                      Text(
                        status,
                        style: TextStyle(
                          color: statusTextColor,
                          fontSize: 12.0.sp(),
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            SizedBox(height: 12.0.h()),

            // Title
            Text(
              tender.title!,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                color: AppColors.grey80,
                fontSize: 16.0.sp(),
                fontWeight: FontWeight.w600,
                height: 1.3,
              ),
            ),
            SizedBox(height: 8.0.h()),

            // Description
            Text(
              tender.description!,
              maxLines: isDesktop ? 2 : 3,
              style: TextStyle(
                color: AppColors.grey70,
                fontSize: 14.0.sp(),
                fontWeight: FontWeight.w400,
                height: 1.4,
              ),
              overflow: TextOverflow.ellipsis,
            ),
            const Spacer(),
            // Bottom section with location and action button
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                // Location with flag
                Expanded(
                  child: Row(
                    children: [
                      SvgPicture.asset(
                        AssetsManager.location,
                        width: 16.0.w(),
                        height: 16.0.h(),
                      ),
                      SizedBox(width: 4.0.w()),
                      Text(
                        tender.countryCode!,
                        style: TextStyle(
                          color: AppColors.grey80,
                          fontSize: 12.0.sp(),
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                      SizedBox(width: 8.0.w()),
                      tender.countryCode != null
                          ? Flag(countryCode: tender.countryCode!)
                          : const Flag(countryCode: ''),
                    ],
                  ),
                ),

                // Self Control button
                status == TenderApprovalStatus.rejected.value
                    ? const SizedBox.shrink()
                    : Container(
                      padding: EdgeInsets.symmetric(
                        horizontal: 16.0.w(),
                        vertical: 4.0.h(),
                      ),
                      decoration: BoxDecoration(
                        color: AppColors.grey20,
                        borderRadius: BorderRadius.circular(88),
                        border: Border.all(color: AppColors.grey40),
                      ),
                      child: Text(
                        submissionMode.isNotEmpty
                            ? submissionMode
                            : tender.procurementTypeCode!,
                        style: TextStyle(
                          color: AppColors.grey60,
                          fontSize: 12.0.sp(),
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
