import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'dart:async';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:superport/core/constants/app_constants.dart';
import 'package:superport/models/company_item_model.dart';
import 'package:superport/screens/common/theme_shadcn.dart';
import 'package:superport/screens/common/components/shadcn_components.dart';
import 'package:superport/screens/common/widgets/pagination.dart';
import 'package:superport/screens/common/widgets/unified_search_bar.dart';
import 'package:superport/screens/common/widgets/standard_action_bar.dart';
import 'package:superport/screens/common/widgets/standard_states.dart';
import 'package:superport/screens/common/layouts/base_list_screen.dart';
import 'package:superport/screens/company/controllers/company_list_controller.dart';
import 'package:superport/screens/company/components/company_tree_view.dart';

/// shadcn/ui 스타일로 재설계된 회사 관리 화면 (통일된 UI 컴포넌트 사용)
class CompanyList extends StatefulWidget {
  const CompanyList({super.key});

  @override
  State<CompanyList> createState() => _CompanyListState();
}

class _CompanyListState extends State<CompanyList> {
  late CompanyListController _controller;
  final TextEditingController _searchController = TextEditingController();
  Timer? _debounceTimer;

  @override
  void initState() {
    super.initState();
    _controller = CompanyListController();
    _controller.initialize(pageSize: AppConstants.companyPageSize);  // 통일된 초기화 방식
  }

  @override
  void dispose() {
    _controller.dispose();
    _searchController.dispose();
    _debounceTimer?.cancel();
    super.dispose();
  }

  /// 검색어 입력 처리 (디바운싱)
  void _onSearchChanged(String value) {
    _debounceTimer?.cancel();
    _debounceTimer = Timer(AppConstants.searchDebounce, () {
      _controller.search(value);  // Controller가 페이지 리셋 처리
    });
  }

  /// 회사 추가 화면으로 이동
  void _navigateToAddScreen() async {
    final result = await Navigator.pushNamed(context, '/company/add');
    if (result == true) {
      _controller.refresh();
    }
  }

  /// 지점 추가 화면으로 이동
  void _navigateToBranchAddScreen() async {
    final result = await Navigator.pushNamed(context, '/company/branch/add');
    if (result == true) {
      _controller.refresh();
    }
  }

  /// 회사 삭제 처리
  void _deleteCompany(int id) {
    showDialog(
      context: context,
      builder: (context) => ShadDialog(
        title: const Text('삭제 확인'),
        description: const Text('이 회사 정보를 삭제하시겠습니까?'),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            ShadButton.outline(
              onPressed: () => Navigator.pop(context),
              child: const Text('취소'),
            ),
            const SizedBox(width: 8),
            ShadButton(
              onPressed: () async {
                Navigator.pop(context);
                try {
                  await _controller.deleteCompany(id);
                  if (mounted) {
                    ShadToaster.of(context).show(
                      const ShadToast(
                        title: Text('성공'),
                        description: Text('회사가 삭제되었습니다.'),
                      ),
                    );
                  }
                } catch (e) {
                  if (mounted) {
                    ShadToaster.of(context).show(
                      ShadToast.destructive(
                        title: const Text('오류'),
                        description: Text(e.toString()),
                      ),
                    );
                  }
                }
              },
              child: const Text('삭제'),
            ),
          ],
        ),
      ),
    );
  }


  /// 지점 삭제 처리
  void _deleteBranch(int companyId, int branchId) {
    showDialog(
      context: context,
      builder: (context) => ShadDialog(
        title: const Text('지점 삭제 확인'),
        description: const Text('이 지점 정보를 삭제하시겠습니까?'),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            ShadButton.outline(
              onPressed: () => Navigator.pop(context),
              child: const Text('취소'),
            ),
            const SizedBox(width: 8),
            ShadButton(
              onPressed: () async {
                Navigator.pop(context);
                try {
                  await _controller.deleteBranch(companyId, branchId);
                  if (mounted) {
                    ShadToaster.of(context).show(
                      const ShadToast(
                        title: Text('성공'),
                        description: Text('지점이 삭제되었습니다.'),
                      ),
                    );
                  }
                } catch (e) {
                  if (mounted) {
                    ShadToaster.of(context).show(
                      ShadToast.destructive(
                        title: const Text('오류'),
                        description: Text(e.toString()),
                      ),
                    );
                  }
                }
              },
              child: const Text('삭제'),
            ),
          ],
        ),
      ),
    );
  }


  /// 본사/지점 구분 배지 생성 - Phase 10: 색체심리학 기반 색상 적용
  Widget _buildCompanyTypeLabel(bool isBranch) {
    return ShadcnBadge(
      text: isBranch ? '지점' : '본사',
      variant: isBranch 
          ? ShadcnBadgeVariant.companyBranch       // Phase 10: 지점 - 밝은 파랑
          : ShadcnBadgeVariant.companyHeadquarters, // Phase 10: 본사 - 진한 파랑  
      size: ShadcnBadgeSize.small,
    );
  }

  /// CompanyItem의 계층적 이름 표시
  Widget _buildDisplayNameText(CompanyItem item) {
    if (item.isBranch) {
      return Text.rich(
        TextSpan(
          children: [
            TextSpan(text: '${item.parentCompanyName} > ', style: ShadcnTheme.bodyMuted),
            TextSpan(text: item.name, style: ShadcnTheme.bodyMedium),
          ],
        ),
      );
    } else {
      return Text(item.name, style: ShadcnTheme.bodyMedium);
    }
  }

  /// 활성 상태 배지 생성
  Widget _buildStatusBadge(bool isActive) {
    return ShadcnBadge(
      text: isActive ? '활성' : '비활성',
      variant: isActive 
          ? ShadcnBadgeVariant.success 
          : ShadcnBadgeVariant.secondary,
      size: ShadcnBadgeSize.small,
    );
  }

  /// 날짜 포맷팅
  String _formatDate(DateTime? date) {
    if (date == null) return '-';
    return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
  }

  /// 담당자 정보 통합 표시 (이름 + 직책)
  Widget _buildContactInfo(CompanyItem item) {
    final name = item.contactName ?? '-';
    final position = item.contactPosition;
    
    if (position != null && position.isNotEmpty) {
      return Text(
        '$name ($position)',
        style: ShadcnTheme.bodySmall.copyWith(fontWeight: FontWeight.w500),
        overflow: TextOverflow.ellipsis,
        maxLines: 1,
      );
    } else {
      return Text(
        name, 
        style: ShadcnTheme.bodySmall,
        overflow: TextOverflow.ellipsis,
        maxLines: 1,
      );
    }
  }

  /// 연락처 정보 통합 표시 (전화 + 이메일)
  Widget _buildContactDetails(CompanyItem item) {
    final phone = item.contactPhone ?? '-';
    final email = item.contactEmail;
    
    if (email != null && email.isNotEmpty) {
      return Text(
        '$phone\n$email',
        style: ShadcnTheme.bodySmall,
        overflow: TextOverflow.ellipsis,
        maxLines: 2,
      );
    } else {
      return Text(
        phone, 
        style: ShadcnTheme.bodySmall,
        overflow: TextOverflow.ellipsis,
        maxLines: 1,
      );
    }
  }

  /// 파트너/고객 플래그 표시
  Widget _buildPartnerCustomerFlags(CompanyItem item) {
    if (item.isBranch) {
      return Text('-', style: ShadcnTheme.bodySmall);
    }
    
    final flags = <Widget>[];
    
    if (item.isPartner) {
      flags.add(ShadcnBadge(
        text: '파트너',
        variant: ShadcnBadgeVariant.companyPartner, // Phase 10: 협력 - 에메랄드
        size: ShadcnBadgeSize.small,
      ));
    }
    
    if (item.isCustomer) {
      flags.add(ShadcnBadge(
        text: '고객',
        variant: ShadcnBadgeVariant.companyCustomer, // Phase 10: 고객 - 진한 그린
        size: ShadcnBadgeSize.small,
      ));
    }
    
    if (flags.isEmpty) {
      return Text('-', style: ShadcnTheme.bodySmall);
    }
    
    return Wrap(
      spacing: 4,
      runSpacing: 2,
      children: flags,
    );
  }

  /// 등록일/수정일 표시
  Widget _buildDateInfo(CompanyItem item) {
    final createdAt = item.createdAt;
    final updatedAt = item.updatedAt;
    
    if (createdAt == null) {
      return Text('-', style: ShadcnTheme.bodySmall);
    }
    
    final created = _formatDate(createdAt);
    
    if (updatedAt != null && updatedAt != createdAt) {
      return Text(
        '등록:$created\n수정:${_formatDate(updatedAt)}',
        style: ShadcnTheme.bodySmall,
        overflow: TextOverflow.ellipsis,
        maxLines: 2,
      );
    } else {
      return Text(
        created, 
        style: ShadcnTheme.bodySmall,
        overflow: TextOverflow.ellipsis,
        maxLines: 1,
      );
    }
  }
  
  // (Deprecated) 기존 커스텀 테이블 빌더 유틸들은 ShadTable 전환으로 제거되었습니다.


  /// ShadTable 기반 회사 테이블 빌더 (기존 컬럼 구성 유지)
  Widget _buildCompanyShadTable(List<CompanyItem> items, CompanyListController controller) {
    if (items.isEmpty) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(Icons.business_outlined, size: 64, color: ShadcnTheme.mutedForeground),
            const SizedBox(height: 16),
            Text('등록된 회사가 없습니다', style: ShadcnTheme.bodyMedium.copyWith(color: ShadcnTheme.mutedForeground)),
          ],
        ),
      );
    }

    return Container(
      decoration: BoxDecoration(
        border: Border.all(color: ShadcnTheme.border),
        borderRadius: BorderRadius.circular(ShadcnTheme.radiusMd),
      ),
      child: Padding(
        padding: const EdgeInsets.all(12.0),
        child: LayoutBuilder(
          builder: (context, constraints) {
            // 최소폭 추정 (다컬럼): 번호(64)+회사명(240)+구분(120)+주소(320)+담당자(200)+연락처(220)+파트너/고객(160)+상태(120)+등록/수정일(200)+비고(220)+관리(160)+여백(24)
            const double actionsW = 160.0;
            const double minTableWidth = 64 + 240 + 120 + 320 + 200 + 220 + 160 + 120 + 200 + 220 + actionsW + 24;
            final double tableWidth = constraints.maxWidth >= minTableWidth
                ? constraints.maxWidth
                : minTableWidth;
            const double addressColumnWidth = 320.0; // 고정, 남는 폭은 filler가 흡수

            return SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: SizedBox(
                width: tableWidth,
                child: ShadTable.list(
                  columnSpanExtent: (index) {
                    switch (index) {
                      case 0:
                        return const FixedTableSpanExtent(64);
                      case 1:
                        return const FixedTableSpanExtent(240);
                      case 2:
                        return const FixedTableSpanExtent(120);
                      case 3:
                        return const FixedTableSpanExtent(addressColumnWidth);
                      case 4:
                        return const FixedTableSpanExtent(200);
                      case 5:
                        return const FixedTableSpanExtent(220);
                      case 6:
                        return const FixedTableSpanExtent(160);
                      case 7:
                        return const FixedTableSpanExtent(120);
                      case 8:
                        return const FixedTableSpanExtent(200);
                      case 9:
                        return const FixedTableSpanExtent(220);
                      case 10:
                        return const FixedTableSpanExtent(actionsW);
                      case 11:
                        return const RemainingTableSpanExtent();
                      default:
                        return const FixedTableSpanExtent(100);
                    }
                  },
                  header: [
                    const ShadTableCell.header(child: Text('번호')),
                    const ShadTableCell.header(child: Text('회사명')),
                    const ShadTableCell.header(child: Text('구분')),
                    ShadTableCell.header(child: SizedBox(width: addressColumnWidth, child: const Text('주소'))),
                    const ShadTableCell.header(child: Text('담당자')),
                    const ShadTableCell.header(child: Text('연락처')),
                    const ShadTableCell.header(child: Text('파트너/고객')),
                    const ShadTableCell.header(child: Text('상태')),
                    const ShadTableCell.header(child: Text('등록/수정일')),
                    const ShadTableCell.header(child: Text('비고')),
                    ShadTableCell.header(child: SizedBox(width: actionsW, child: const Text('관리'))),
                    const ShadTableCell.header(child: SizedBox.shrink()),
                  ],
                  children: [
                    for (int index = 0; index < items.length; index++)
                      [
                        // 번호
                        ShadTableCell(child: Text(((((controller.currentPage - 1) * controller.pageSize) + index + 1)).toString(), style: ShadcnTheme.bodySmall)),
                        // 회사명 (본사>지점 표기 유지)
                        ShadTableCell(child: _buildDisplayNameText(items[index])),
                        // 구분 (본사/지점)
                        ShadTableCell(child: _buildCompanyTypeLabel(items[index].isBranch)),
                        // 주소 (가변 폭)
                        ShadTableCell(
                          child: SizedBox(
                            width: addressColumnWidth,
                            child: Text(items[index].address.isNotEmpty ? items[index].address : '-', overflow: TextOverflow.ellipsis, style: ShadcnTheme.bodySmall),
                          ),
                        ),
                        // 담당자 요약
                        ShadTableCell(child: _buildContactInfo(items[index])),
                        // 연락처 상세
                        ShadTableCell(child: _buildContactDetails(items[index])),
                        // 파트너/고객 플래그
                        ShadTableCell(child: _buildPartnerCustomerFlags(items[index])),
                        // 상태
                        ShadTableCell(child: _buildStatusBadge(items[index].isActive)),
                        // 등록/수정일
                        ShadTableCell(child: _buildDateInfo(items[index])),
                        // 비고
                        ShadTableCell(child: Text(items[index].remark ?? '-', overflow: TextOverflow.ellipsis, style: ShadcnTheme.bodySmall)),
                        // 관리(편집/삭제)
                        ShadTableCell(
                          child: SizedBox(
                            width: actionsW,
                            child: Row(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                if (items[index].id != null) ...[
                                  ShadButton.ghost(
                                    size: ShadButtonSize.sm,
                                    onPressed: () async {
                                      // 기존 편집 흐름 유지
                                      final args = {'companyId': items[index].id};
                                      final result = await Navigator.pushNamed(context, '/company/edit', arguments: args);
                                      if (result == true) {
                                        controller.refresh();
                                      }
                                    },
                                    child: const Icon(Icons.edit, size: 16),
                                  ),
                                  const SizedBox(width: 4),
                                  ShadButton.ghost(
                                    size: ShadButtonSize.sm,
                                    onPressed: () {
                                      if (items[index].isBranch) {
                                        _deleteBranch(items[index].parentCompanyId!, items[index].id!);
                                      } else {
                                        _deleteCompany(items[index].id!);
                                      }
                                    },
                                    child: const Icon(Icons.delete, size: 16),
                                  ),
                                ],
                              ],
                            ),
                          ),
                        ),
                        const ShadTableCell(child: SizedBox.shrink()),
                      ],
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }
  
  /// Tree View 빌드
  Widget _buildTreeView(BuildContext context, CompanyListController controller) {
    if (controller.companyHierarchy == null) {
      return const StandardLoadingState(message: '계층 구조를 불러오는 중...');
    }
    
    return CompanyTreeView(
      hierarchy: controller.companyHierarchy!,
      expandedNodes: controller.expandedNodes,
      onToggleExpand: controller.toggleNodeExpansion,
      onNodeTap: (nodeId) {
        // 회사 상세 화면으로 이동
        Navigator.pushNamed(
          context, 
          '/company/edit', 
          arguments: {'companyId': int.parse(nodeId)},
        );
      },
      onEdit: (nodeId) {
        Navigator.pushNamed(
          context,
          '/company/edit',
          arguments: {'companyId': int.parse(nodeId)},
        );
      },
      onDelete: (nodeId) async {
        final companyId = int.parse(nodeId);
        // 삭제 가능 여부 먼저 확인
        final canDelete = await controller.canDeleteCompany(companyId);
        if (canDelete) {
          _deleteCompany(companyId);
        } else {
          if (mounted) {
            ShadToaster.of(context).show(
              const ShadToast(
                title: Text('삭제 불가'),
                description: Text('자식 회사가 있어 삭제할 수 없습니다.'),
              ),
            );
          }
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider.value(
      value: _controller,
      child: Consumer<CompanyListController>(
        builder: (context, controller, child) {
          // CompanyItem 데이터 직접 사용 (복잡한 변환 로직 제거)
          final companyItems = controller.companyItems;
          final int totalCount = controller.total;
          final int actualHeadquartersCount = controller.actualHeadquartersCount;
          final int actualBranchesCount = totalCount - actualHeadquartersCount; // 지점 개수 = 전체 - 본사
          final int displayedHeadquartersCount = controller.displayedHeadquartersCount;
          final int displayedBranchesCount = companyItems.where((item) => item.isBranch).length;
          
          print('🔍 [VIEW DEBUG] CompanyItem 페이지네이션 상태');
          print('  • CompanyItem items: ${controller.companyItems.length}개');
          print('  • 전체 개수: ${controller.total}개');
          print('  • 실제 본사 개수(API): $actualHeadquartersCount개');
          print('  • 실제 지점 개수(계산): $actualBranchesCount개');
          print('  • 표시된 본사 개수: $displayedHeadquartersCount개');
          print('  • 표시된 지점 개수: $displayedBranchesCount개');
          print('  • 현재 페이지: ${controller.currentPage}');
          print('  • 페이지 크기: ${controller.pageSize}');

          // 로딩 상태
          if (controller.isLoading && controller.companyItems.isEmpty) {
            return const StandardLoadingState(message: '회사 데이터를 불러오는 중...');
          }

          return BaseListScreen(
            isLoading: false,
            error: controller.error,
            onRefresh: controller.refresh,
            emptyMessage:
                controller.searchQuery.isNotEmpty
                    ? '검색 결과가 없습니다'
                    : '등록된 회사가 없습니다',
            emptyIcon: Icons.business_outlined,

            // 검색바
            searchBar: UnifiedSearchBar(
              controller: _searchController,
              placeholder: '회사명, 담당자명, 연락처로 검색',
              onChanged: _onSearchChanged, // 실시간 검색 (디바운싱)
              onSearch:
                  () => _controller.search(
                    _searchController.text,
                  ), // 즉시 검색
              onClear: () {
                _searchController.clear();
                _onSearchChanged('');
              },
            ),

            // 액션바
            actionBar: StandardActionBar(
              leftActions: [
                // 회사 추가 버튼
                StandardActionButtons.addButton(
                  text: '회사 추가',
                  onPressed: _navigateToAddScreen,
                ),
                // 지점 추가 버튼
                StandardActionButtons.addButton(
                  text: '지점 추가',
                  onPressed: _navigateToBranchAddScreen,
                  icon: Icons.domain_add,
                ),
              ],
              rightActions: [
                // Tree View 토글 버튼
                IconButton(
                  icon: Icon(
                    controller.isTreeView ? Icons.list : Icons.account_tree,
                    color: controller.isTreeView ? ShadcnTheme.primary : null,
                  ),
                  tooltip: controller.isTreeView ? '리스트 보기' : '계층 보기',
                  onPressed: () => controller.toggleTreeView(),
                ),
                const SizedBox(width: 8),
                // 관리자용 비활성 포함 체크박스
                // TODO: 실제 권한 체크 로직 추가 필요
                Row(
                  children: [
                    Checkbox(
                      value: controller.includeInactive,
                      onChanged: (_) => controller.toggleIncludeInactive(),
                    ),
                    const Text('비활성 포함'),
                  ],
                ),
              ],
              totalCount: totalCount,  // 전체 회사 수 (본사 + 지점)
              onRefresh: controller.refresh,
              statusMessage: controller.searchQuery.isNotEmpty
                  ? '"${controller.searchQuery}" 검색 결과'
                  : actualHeadquartersCount > 0 
                      ? '본사: $actualHeadquartersCount개, 지점: $actualBranchesCount개 총 $totalCount개'
                      : null,
            ),

            // 에러 메시지
            filterSection:
                controller.error != null
                    ? StandardInfoMessage(
                      message: controller.error!,
                      icon: Icons.error_outline,
                      color: ShadcnTheme.destructive,
                      onClose: controller.clearError,
                    )
                    : null,

            // 데이터 테이블 또는 Tree View
            dataTable: controller.isTreeView
                ? _buildTreeView(context, controller)
                : companyItems.isEmpty
                    ? StandardEmptyState(
                      title:
                          controller.searchQuery.isNotEmpty
                              ? '검색 결과가 없습니다'
                              : '등록된 회사가 없습니다',
                      icon: Icons.business_outlined,
                      action:
                          controller.searchQuery.isEmpty
                              ? StandardActionButtons.addButton(
                                text: '첫 회사 추가하기',
                                onPressed: _navigateToAddScreen,
                              )
                              : null,
                    )
                    : _buildCompanyShadTable(companyItems, controller),

            // 페이지네이션 (BaseListController의 goToPage 사용)
            pagination: Pagination(
              totalCount: controller.total,
              currentPage: controller.currentPage,
              pageSize: controller.pageSize,
              onPageChanged: (page) {
                controller.goToPage(page);
              },
            ),
            dataAreaPadding: EdgeInsets.zero,
          );
        },
      ),
    );
  }
}
