import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.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/theme/colors.dart';
import '../../../core/utils/size_config.dart';
import '../../../view_models/your_tenders_view_model.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) {
    final viewModel = context.read<YourTendersViewModel>();

    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: viewModel.getBackGroundColorByStatus(status),
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: viewModel.getBorderColorByStatus(status)),
      ),
      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: viewModel.getStatusCardColorByStatus(status),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Row(
                    children: [
                      if (viewModel.getStatusIconByStatus(status) != null)
                        SvgPicture.asset(
                          viewModel.getStatusIconByStatus(status)!,
                          width: 16.0.w(),
                          height: 16.0.h(),
                          colorFilter: ColorFilter.mode(
                            viewModel.getStatusTextColorByStatus(status),
                            BlendMode.srcIn,
                          ),
                        ),
                      SizedBox(width: 4.0.w()),
                      Text(
                        status,
                        style: TextStyle(
                          color: viewModel.getStatusTextColorByStatus(status),
                          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,
                        ),
                      ),
                    ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
