import 'package:flutter/material.dart';

import '../widgets/app_gradients.dart';
import '../widgets/entrance.dart';

const Color _pink = Color(0xFFFF55A7);
const Color _teal = Color(0xFF2F9E94);

class _CreditPerson {
  const _CreditPerson(this.name, this.role, {this.logoPath});

  final String name;
  final String role;

  /// Quando definido, mostra este logótipo em vez do círculo com a
  /// inicial do nome — usado na instituição colaboradora.
  final String? logoPath;
}

class _CreditSection {
  const _CreditSection(this.label, this.people);

  final String label;
  final List<_CreditPerson> people;
}

const List<_CreditSection> _kCreditSections = [
  _CreditSection('Criadora original', [
    _CreditPerson('Francisca Salgado Ferreira Pacheco Silva', 'Criadora original'),
  ]),
  _CreditSection('Desenvolvimento informático', [
    _CreditPerson('Carlos Correia', 'Desenvolvedor'),
    _CreditPerson('Fábio Ceia', 'Desenvolvedor'),
    _CreditPerson('Ruben Grandra', 'Desenvolvedor'),
    _CreditPerson('Dinis Maria', 'Desenvolvedor'),
  ]),
  _CreditSection('Orientadores', [
    _CreditPerson('Augusta Pureza Alves Silveira', 'Orientador(a)'),
    _CreditPerson('Cristina Lopes Cardoso Silva', 'Orientador(a)'),
    _CreditPerson('João Carlos Rodrigues L. Miranda', 'Orientador(a)'),
  ]),
  _CreditSection('Instituição colaboradora', [
    _CreditPerson('Universidade Fernando Pessoa', ''),
    _CreditPerson(
      'Escola Profissional de Vila do Conde',
      '',
      logoPath: 'assets/logo2.png',
    ),
  ]),
];

class CreditsScreen extends StatelessWidget {
  const CreditsScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: const Size.fromHeight(kToolbarHeight),
        child: Container(
          decoration: const BoxDecoration(gradient: kAppBarGradient),
          child: AppBar(
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.white,
            surfaceTintColor: Colors.transparent,
            elevation: 0,
            scrolledUnderElevation: 0,
            title: const Text(
              'Criadores e Colaboradores',
              style: TextStyle(fontWeight: FontWeight.w900),
            ),
          ),
        ),
      ),
      body: Container(
        color: const Color(0xFFFAFAF7),
        child: SafeArea(
          child: SingleChildScrollView(
            padding: const EdgeInsets.fromLTRB(20, 24, 20, 24),
            child: Column(
              children: [
                FadeSlideIn(
                  child: Column(
                    children: [
                      Container(
                        width: 76,
                        height: 76,
                        decoration: BoxDecoration(
                          color: _pink.withValues(alpha: 0.12),
                          shape: BoxShape.circle,
                        ),
                        child: const Icon(
                          Icons.diversity_3_rounded,
                          color: _pink,
                          size: 36,
                        ),
                      ),
                      const SizedBox(height: 16),
                      const Text(
                        'Quem fez a Check-Teeth Kids',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.w900,
                          color: Colors.black,
                        ),
                      ),
                    ],
                  ),
                ),
                const SizedBox(height: 22),
                for (var s = 0; s < _kCreditSections.length; s++) ...[
                  if (s > 0) const SizedBox(height: 18),
                  FadeSlideIn(
                    delay: Duration(milliseconds: 60 * (s + 1)),
                    child: _CreditSectionCard(section: _kCreditSections[s]),
                  ),
                ],
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class _CreditSectionCard extends StatelessWidget {
  const _CreditSectionCard({required this.section});

  final _CreditSection section;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        Padding(
          padding: const EdgeInsets.only(left: 4, bottom: 8),
          child: Text(
            section.label,
            style: const TextStyle(
              color: _teal,
              fontWeight: FontWeight.w900,
              fontSize: 14,
            ),
          ),
        ),
        Container(
          padding: const EdgeInsets.symmetric(vertical: 6),
          decoration: BoxDecoration(
            color: Colors.white.withValues(alpha: 0.9),
            borderRadius: BorderRadius.circular(20),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withValues(alpha: 0.05),
                blurRadius: 14,
                offset: const Offset(0, 6),
              ),
            ],
          ),
          child: Column(
            children: [
              for (var i = 0; i < section.people.length; i++) ...[
                if (i > 0) const Divider(height: 1, indent: 18, endIndent: 18),
                _CreditPersonRow(person: section.people[i]),
              ],
            ],
          ),
        ),
      ],
    );
  }
}

class _CreditPersonRow extends StatelessWidget {
  const _CreditPersonRow({required this.person});

  final _CreditPerson person;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
      child: Row(
        children: [
          Container(
            width: 40,
            height: 40,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: _teal.withValues(alpha: 0.12),
              shape: BoxShape.circle,
            ),
            clipBehavior: Clip.antiAlias,
            child: person.logoPath != null
                ? Padding(
                    padding: const EdgeInsets.all(6),
                    child: Image.asset(
                      person.logoPath!,
                      fit: BoxFit.contain,
                    ),
                  )
                : Text(
                    person.name[0].toUpperCase(),
                    style: const TextStyle(
                      color: _teal,
                      fontWeight: FontWeight.w900,
                      fontSize: 16,
                    ),
                  ),
          ),
          const SizedBox(width: 14),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  person.name,
                  style: const TextStyle(
                    fontWeight: FontWeight.w800,
                    fontSize: 14.5,
                    color: Colors.black87,
                  ),
                ),
                if (person.role.isNotEmpty) ...[
                  const SizedBox(height: 2),
                  Text(
                    person.role,
                    style: TextStyle(
                      fontWeight: FontWeight.w600,
                      fontSize: 12.5,
                      color: Colors.black.withValues(alpha: 0.55),
                    ),
                  ),
                ],
              ],
            ),
          ),
        ],
      ),
    );
  }
}
