<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\PontoFocal;
use App\Models\Structure;
use Illuminate\Support\Str;

class PontosFocaisSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Limpar pontos focais existentes antes de inserir (se executado via artisan)
        if ($this->command) {
            $count = PontoFocal::count();
            if ($count > 0) {
                if ($this->command->confirm("Existem {$count} ponto(s) focal(is) no banco. Deseja limpar antes de inserir?", true)) {
                    PontoFocal::truncate();
                    $this->command->info("🗑️  {$count} ponto(s) focal(is) removido(s).");
                }
            }
        }
        // Mapeamento de instituições do arquivo blade para buscar no banco
        $instituicoes = [
            'Instituto de Previdência Social do Município de Angra dos Reis (ANGRAPREV)' => ['ANGRAPREV'],
            'Controladoria Geral do Município (CGM)' => ['CGM'],
            'Fundação de Turismo de Angra dos Reis (TURISANGRA)' => ['FTAR'], // Corrigido: TURISANGRA -> FTAR
            'Instituto Municipal do Ambiente de Angra dos Reis (IMAAR)' => ['IMAAR'],
            'Procuradoria Geral do Município (PGM)' => ['PGM'],
            'Serviço Autônomo de Captação de Água e Tratamento de Esgoto (SAAE)' => ['SAAE'],
            'Secretaria de Gestão de Suprimentos (SGES)' => ['SGES'],
            'Secretaria de Modernização e Gestão de Pessoal (SMGP)' => ['SMGP', 'SAD'],
            'Secretaria de Agricultura, Aquicultura e Pesca (SAAP)' => ['SAAP'],
            'Secretaria de Cultura e Patrimônio (SCP)' => ['SCP'],
            'Secretaria de Desenvolvimento Econômico (SDE)' => ['SDE'],
            'Secretaria de Desenvolvimento Social e Promoção da Cidadania (SDSP)' => ['SDSP'],
            'Secretaria de Desenvolvimento Regional (SDR)' => ['SDR'],
            'Secretaria de Educação, Juventude e Inovação (SEJIN)' => ['SEJIN'],
            'Secretaria de Esporte e Lazer (SEL)' => ['SEL'],
            'Secretaria de Finanças (SFI)' => ['SFI'],
            'Secretaria de Parcerias e Inovação Tecnológica (SPIT)' => ['SPIT'],
            'Secretaria de Articulação Governamental (SAG)' => ['SAG'],
            'Secretaria de Relações Institucionais (SRI)' => ['SRI'],
            'Secretaria de Proteção e Defesa Civil (SPDC)' => ['SPDC'],
            'Superintendência De Gestão e Articulação (SUGEA)' => ['SPDC'], // SUGEA é setor da SPDC
            'Secretaria de Planejamento e Gestão (SPG)' => ['SPG'],
            'Secretaria Municipal de Saúde (SSA)' => ['SE'], // Corrigido: SSA -> SE
            'Secretaria de Segurança Pública (SSP)' => ['SSP'],
            'Secretaria de Urbanização, Parques e Jardins (SUPJ)' => ['SUPJ'],
            'Secretaria Extraordinária de Infraestrutura (SEINF)' => ['SEINF'],
            'Secretaria de Obras e Habitação (SOH)' => ['SOH'],
            'Coordenação Técnica de Pregão (CTPRE)' => ['SGES'], // CTPRE é setor da SGES
        ];

        // Função auxiliar para encontrar structure existente no banco
        $findStructure = function($nome) use ($instituicoes) {
            // Primeiro, tentar encontrar pelo mapeamento usando sigla (abbreviation)
            foreach ($instituicoes as $nomeCompleto => $siglas) {
                if (str_contains($nome, $nomeCompleto) || str_contains($nomeCompleto, $nome)) {
                    foreach ($siglas as $sigla) {
                        // Buscar por sigla (abbreviation) - mais confiável (sem restrição de status)
                        $structure = Structure::where('abbreviation', $sigla)->first();
                        if ($structure) {
                            return $structure;
                        }
                    }
                }
            }

            // Tentar extrair sigla do nome entre parênteses
            $parts = explode('(', $nome);
            if (count($parts) > 1) {
                $sigla = trim(str_replace(')', '', $parts[1]));
                $structure = Structure::where('abbreviation', $sigla)->first();
                if ($structure) {
                    return $structure;
                }
            }

            // Buscar por nome completo ou parcial (sem restrição de status)
            foreach ($instituicoes as $nomeCompleto => $siglas) {
                if (str_contains($nome, $nomeCompleto) || str_contains($nomeCompleto, $nome)) {
                    $structure = Structure::where('name', 'like', "%{$nomeCompleto}%")->first();
                    if ($structure) {
                        return $structure;
                    }
                }
            }

            // Última tentativa: busca parcial por qualquer parte do nome
            $structure = Structure::where('name', 'like', "%{$nome}%")->first();
            if ($structure) {
                return $structure;
            }

            return null;
        };

        // Dados dos pontos focais do arquivo blade
        $pontosFocais = [

            // ANGRAPREV
            ['instituicao' => 'Instituto de Previdência Social do Município de Angra dos Reis (ANGRAPREV)', 'nome' => 'Camille Gomes Dourado', 'matricula' => '2500275', 'telefone_ramal' => '1778'],
            ['instituicao' => 'Instituto de Previdência Social do Município de Angra dos Reis (ANGRAPREV)', 'nome' => 'Paulo Henrique da Silva Bulé', 'matricula' => '17345', 'telefone_ramal' => '1287'],

            // CGM
            ['instituicao' => 'Controladoria Geral do Município (CGM)', 'nome' => 'Magda Neves Angelo', 'matricula' => '16972', 'telefone_ramal' => '1166', 'telefone' => '(24) 3365-6131', 'email' => 'cgm.cgab@angra.rj.gov.br'],
            ['instituicao' => 'Controladoria Geral do Município (CGM)', 'nome' => 'Jobson Rodrigues da Silva', 'matricula' => '19706', 'telefone_ramal' => '1172', 'telefone' => '(24) 3377-8302'],
            ['instituicao' => 'Controladoria Geral do Município (CGM)', 'nome' => 'Sandro Campos Sales', 'matricula' => '29268', 'telefone' => '(24) 3377-8302'],
            ['instituicao' => 'Controladoria Geral do Município (CGM)', 'nome' => 'Bernadete Guedes Correia', 'matricula' => '3296', 'telefone' => '(24) 3377-8302'],

            // TURISANGRA
            ['instituicao' => 'Fundação de Turismo de Angra dos Reis (TURISANGRA)', 'nome' => 'Christian da Silva Galois', 'matricula' => '190341', 'telefone_ramal' => '7711', 'telefone' => '(24) 3369-7711', 'email' => 'tur.citt@angra.rj.gov'],
            ['instituicao' => 'Fundação de Turismo de Angra dos Reis (TURISANGRA)', 'nome' => 'Rosângela de Oliveira Lima', 'matricula' => '3945', 'telefone_ramal' => '1294', 'telefone' => '(24) 3367-7789', 'email' => 'tur.copes@angra.rj.gov'],

            // IMAAR
            ['instituicao' => 'Instituto Municipal do Ambiente de Angra dos Reis (IMAAR)', 'nome' => 'Vinicíos Júdice dos Santos', 'matricula' => '19075', 'telefone_ramal' => '1041', 'telefone' => '(24) 3368-6418', 'email' => 'imaar.dprot@angra.rj.gov'],
            ['instituicao' => 'Instituto Municipal do Ambiente de Angra dos Reis (IMAAR)', 'nome' => 'Bruno Reis de Lima', 'matricula' => '19062', 'telefone_ramal' => '6418', 'telefone' => '(24) 3368-6418', 'email' => 'breislim@gmail.com'],

            // PGM
            ['instituicao' => 'Procuradoria Geral do Município (PGM)', 'nome' => 'Bárbara Di Sarli', 'matricula' => '32362', 'telefone' => '(24) 97835-7022'],
            ['instituicao' => 'Procuradoria Geral do Município (PGM)', 'nome' => 'Eduarda Arruda', 'matricula' => '29509', 'telefone' => '(24) 99912-2160'],
            ['instituicao' => 'Procuradoria Geral do Município (PGM)', 'nome' => 'Rafaela Pimenta', 'matricula' => '32602', 'telefone' => '(24) 99957-5211'],

            // SAAE
            ['instituicao' => 'Serviço Autônomo de Captação de Água e Tratamento de Esgoto (SAAE)', 'nome' => 'Leonardo Lopes Barbosa', 'matricula' => '190379', 'telefone_ramal' => '1577'],
            ['instituicao' => 'Serviço Autônomo de Captação de Água e Tratamento de Esgoto (SAAE)', 'nome' => 'Antônio Carlos Ferreira Soares Junior', 'matricula' => '191107', 'telefone_ramal' => '1577', 'email' => 'antonio.soares@saaeangra.com.br'],

            // SGES
            ['instituicao' => 'Secretaria de Gestão de Suprimentos (SGES)', 'nome' => 'Vera Lúcia Amaral Felipe', 'matricula' => '26729', 'telefone_ramal' => '1156', 'telefone' => '(24) 3365-6439', 'email' => 'atasderegistro@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Gestão de Suprimentos (SGES)', 'nome' => 'Willian Barbosa da Costa', 'matricula' => '20436', 'telefone_ramal' => '1163', 'telefone' => '(24) 3377-7906', 'email' => 'adpe@angra.rj.gov'],

            // SMGP
            ['instituicao' => 'Secretaria de Modernização e Gestão de Pessoal (SMGP)', 'nome' => 'Benedito Paulo Pereira Cascardo', 'matricula' => '19740', 'telefone_ramal' => '5396', 'telefone' => '(24) 3365-5396'],
            ['instituicao' => 'Secretaria de Modernização e Gestão de Pessoal (SMGP)', 'nome' => 'Daniel do Carmo Neves', 'matricula' => '3391', 'telefone_ramal' => '1074', 'telefone' => '(24) 3365-7415', 'email' => 'sad.dgnp@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Modernização e Gestão de Pessoal (SMGP)', 'nome' => 'Carlos Eduardo Borges Campanário', 'matricula' => '18075', 'telefone_ramal' => '1479', 'telefone' => '(24) 3365-5391', 'email' => 'adpe@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Modernização e Gestão de Pessoal (SMGP)', 'nome' => 'Cristiane Viteldo Braz Waleriano', 'matricula' => '14222', 'telefone_ramal' => '1471', 'telefone' => '(24) 3365-5156', 'email' => 'asfp@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Modernização e Gestão de Pessoal (SMGP)', 'nome' => 'Suzana Lyra Soares', 'matricula' => '19878', 'telefone_ramal' => '1484', 'telefone' => '(24) 3377-8310', 'email' => 'rh.tec@angra.rj.gov'],

            // SAAP
            ['instituicao' => 'Secretaria de Agricultura, Aquicultura e Pesca (SAAP)', 'nome' => 'Filipe Nascimento Albano', 'matricula' => '32633', 'telefone_ramal' => '1839', 'email' => 'secretaria.saap@angra.rj.gov.br'],
            ['instituicao' => 'Secretaria de Agricultura, Aquicultura e Pesca (SAAP)', 'nome' => 'Geórgia dos Santos Domingos Maia', 'matricula' => '4502408', 'telefone_ramal' => '1020'],
            ['instituicao' => 'Secretaria de Agricultura, Aquicultura e Pesca (SAAP)', 'nome' => 'André Luis Malvão de Sousa', 'matricula' => '3473', 'telefone_ramal' => '1854'],

            // SCP
            ['instituicao' => 'Secretaria de Cultura e Patrimônio (SCP)', 'nome' => 'Roberta Nakamashi', 'matricula' => '30502', 'email' => 'fundo.cultura@angra.rj.gov.br', 'telefone' => '(24) 3365-7200'],
            ['instituicao' => 'Secretaria de Cultura e Patrimônio (SCP)', 'nome' => 'Samir Machado Lessa', 'matricula' => '29586', 'email' => 'coleta.cultura@angra.rj.gov.br', 'telefone' => '(24) 3365-7200'],

            // SDE
            ['instituicao' => 'Secretaria de Desenvolvimento Econômico (SDE)', 'nome' => 'Daniele Vilela dos Santos', 'matricula' => '33125', 'telefone_ramal' => '1616', 'telefone' => '(24) 3365-0209', 'email' => 'seics@angra.rj.gov.br'],
            ['instituicao' => 'Secretaria de Desenvolvimento Econômico (SDE)', 'nome' => 'Josua Pereira de Lima Junior', 'matricula' => '1416', 'telefone_ramal' => '4507', 'telefone' => '(24) 3377-4507', 'email' => 'sde.decin@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Desenvolvimento Econômico (SDE)', 'nome' => 'Wolner Goes do Rosario', 'matricula' => '19656', 'telefone_ramal' => '4507', 'telefone' => '(24) 998397447', 'email' => 'wgr.rosario@gmail.com'],

            // SDSP
            ['instituicao' => 'Secretaria de Desenvolvimento Social e Promoção da Cidadania (SDSP)', 'nome' => 'Arão de Assis Carrilho', 'matricula' => '14522693788', 'telefone_ramal' => '1240', 'email' => 'sdsp.ctvs@angra.rj.gov.br'],
            ['instituicao' => 'Secretaria de Desenvolvimento Social e Promoção da Cidadania (SDSP)', 'nome' => 'Ana Isabella Oliveira dos Santos', 'matricula' => '------', 'telefone_ramal' => '1240', 'email' => 'sdsp@angra.rj.gov.br'],

            // SDR
            ['instituicao' => 'Secretaria de Desenvolvimento Regional (SDR)', 'nome' => 'Dulcineia Gil da Silva', 'matricula' => '21819', 'telefone' => '(24) 99843-8866'],
            ['instituicao' => 'Secretaria de Desenvolvimento Regional (SDR)', 'nome' => 'Hugo Marques Machado Ferreira', 'matricula' => '30950', 'telefone' => '(24) 99832-0236'],
            ['instituicao' => 'Secretaria de Desenvolvimento Regional (SDR)', 'nome' => 'Esabellen Maia Ramos Barros', 'matricula' => '32761', 'telefone_ramal' => '1184', 'telefone' => '24992761376'],
            ['instituicao' => 'Secretaria de Desenvolvimento Regional (SDR)', 'nome' => 'Helena Amaral de Souza', 'matricula' => '22215', 'telefone' => '33623633', 'telefone_ramal' => '1526'],
            ['instituicao' => 'Secretaria de Desenvolvimento Regional (SDR)', 'nome' => 'Gustavo da Silva Marinho', 'matricula' => '29121', 'telefone' => '(24) 98845-4018'],

            // SEJIN
            ['instituicao' => 'Secretaria de Educação, Juventude e Inovação (SEJIN)', 'nome' => 'Raphael Carlos Pessanha do Rosário', 'matricula' => '17384', 'telefone_ramal' => '1022', 'telefone' => '(24) 99962-0124', 'email' => 'sect.cocti@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Educação, Juventude e Inovação (SEJIN)', 'nome' => 'Danielle Noronha de Melo', 'matricula' => '22449', 'telefone_ramal' => '1022', 'telefone' => '(24) 98809-2094', 'email' => 'sect.cocti@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Educação, Juventude e Inovação (SEJIN)', 'nome' => 'Diller Oséias Lara da Silva', 'matricula' => '10608', 'telefone_ramal' => '1022', 'telefone' => '(24) 3379-9922'],
            ['instituicao' => 'Secretaria de Educação, Juventude e Inovação (SEJIN)', 'nome' => 'Dayanne Cristhine de Assis Alves Pereira', 'matricula' => '26902', 'telefone' => '24 99269-0066'],

            // SEL
            ['instituicao' => 'Secretaria de Esporte e Lazer (SEL)', 'nome' => 'Felipe de Assis Teixeira', 'matricula' => '11744', 'telefone_ramal' => '3224', 'telefone' => '(24) 3365-3224', 'email' => 'projetosesportivos@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Esporte e Lazer (SEL)', 'nome' => 'Iderlan Cadilha Cunha', 'matricula' => '32634'],
            ['instituicao' => 'Secretaria de Esporte e Lazer (SEL)', 'nome' => 'Denise Monteiro da Fonseca Martins', 'matricula' => '4501473'],

            // SFI
            ['instituicao' => 'Secretaria de Finanças (SFI)', 'nome' => 'Claudia Porto de Arroxelas Bragança', 'matricula' => '22299', 'telefone_ramal' => '1086', 'telefone' => '(24) 3365-6472', 'email' => 'sfi.gt@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Finanças (SFI)', 'nome' => 'Bianca Luziane Queiroz Godinho', 'matricula' => '12359', 'telefone_ramal' => '1768', 'telefone' => '(24) 3365-6466', 'email' => 'sfi.aci@angra.rj.gov'],

            // SPIT
            ['instituicao' => 'Secretaria de Parcerias e Inovação Tecnológica (SPIT)', 'nome' => 'Lourival Cutrim Gomes Neto', 'matricula' => '32739', 'telefone' => '(61) 99559-2868', 'email' => 'parceriaseinovacao@angra.rj.gov.br'],
            ['instituicao' => 'Secretaria de Parcerias e Inovação Tecnológica (SPIT)', 'nome' => 'Marcio Loureiro Taveira', 'matricula' => '701865128', 'telefone' => '(21) 98430-6229', 'email' => 'parceriaseinovacao@angra.rj.gov.br'],

            // SAG
            ['instituicao' => 'Secretaria de Articulação Governamental (SAG)', 'nome' => 'Renan de Andrade Leone', 'matricula' => '22391', 'telefone_ramal' => '1116', 'telefone' => '(24) 3365-1234', 'email' => 'ouvidoria@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Articulação Governamental (SAG)', 'nome' => 'Jaqueline Ferreira de Araújo', 'matricula' => '32365', 'telefone_ramal' => '1817', 'telefone' => '(24) 3365-7174', 'email' => 'governo.apoio03@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Articulação Governamental (SAG)', 'nome' => 'Annelise Katiusca Carvalho da Silva', 'matricula' => '29362', 'telefone' => '(24) 99972-8237', 'email' => 'comunicacao@angra.rj.gov.br'],

            // SRI
            ['instituicao' => 'Secretaria de Relações Institucionais (SRI)', 'nome' => 'Adriana Teixeira da Silva', 'matricula' => '19162', 'telefone_ramal' => '1152'],
            ['instituicao' => 'Secretaria de Relações Institucionais (SRI)', 'nome' => 'Luana Ferreira da Rosa Correa', 'matricula' => '32678', 'telefone_ramal' => '1152'],

            // SPDC
            ['instituicao' => 'Secretaria de Proteção e Defesa Civil (SPDC)', 'nome' => 'Carla Rosiane Lima', 'matricula' => '11867', 'telefone_ramal' => '1761', 'telefone' => '(24) 3377-6046', 'email' => 'defesacivil@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Proteção e Defesa Civil (SPDC)', 'nome' => 'Gislaine de Oliveira Freitas da Silva', 'matricula' => '17946', 'telefone_ramal' => '1144', 'telefone' => '(24) 3377-6152', 'email' => 'administracaodc@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Proteção e Defesa Civil (SPDC)', 'nome' => 'Maria de Fátima Meirelles Salgado', 'matricula' => '3307', 'telefone_ramal' => '1142', 'telefone' => '(24) 3365-2213', 'email' => 'controleinternodc@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Proteção e Defesa Civil (SPDC)', 'nome' => 'Maykon da Silva de Oliveira', 'matricula' => '11757', 'telefone_ramal' => '1146', 'telefone' => '(24) 3377-8737', 'email' => 'engenhariadc@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Proteção e Defesa Civil (SPDC)', 'nome' => 'Giuliano Machado Costa', 'matricula' => '11740', 'telefone_ramal' => '1132', 'telefone' => '(24) 3377-9217', 'email' => 'operacoesdc@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Proteção e Defesa Civil (SPDC)', 'nome' => 'Carlos Ramos Tomaz', 'matricula' => '30118', 'telefone' => '(24) 99201-4000'],
            ['instituicao' => 'Secretaria de Proteção e Defesa Civil (SPDC)', 'nome' => 'Carla Roseane Lima', 'matricula' => '11867', 'telefone_ramal' => '1761'],

            // SUGEA
            ['instituicao' => 'Superintendência De Gestão e Articulação (SUGEA)', 'nome' => 'Carlos Ramos Tomaz', 'matricula' => '30118', 'telefone' => '(24) 99201-4000'],
            ['instituicao' => 'Superintendência De Gestão e Articulação (SUGEA)', 'nome' => 'Carla Roseane Lima', 'matricula' => '11867', 'telefone_ramal' => '1761'],

            // SPG
            ['instituicao' => 'Secretaria de Planejamento e Gestão (SPG)', 'nome' => 'Maria Carolina Carvalho dos Santos', 'matricula' => '30845', 'telefone_ramal' => '1212', 'telefone' => '(24) 3365-1212', 'email' => 'administrativo.pge@angra.rj.gov'],
            ['instituicao' => 'Secretaria de Planejamento e Gestão (SPG)', 'nome' => 'Advanice Cirino Queiroz', 'matricula' => '33257', 'telefone_ramal' => '1106'],

            // SSA
            ['instituicao' => 'Secretaria Municipal de Saúde (SSA)', 'nome' => 'Thiago Omena de Carvalho', 'matricula' => '14858', 'telefone_ramal' => '1198', 'telefone' => '(24) 3364-4312', 'email' => 'fusarcpd@angra.rj.gov'],
            ['instituicao' => 'Secretaria Municipal de Saúde (SSA)', 'nome' => 'Renata Fernandes Silva dos Santos Braga', 'matricula' => '26180', 'telefone_ramal' => '2014', 'telefone' => '(24) 3365-2517', 'email' => 'ssa.corh@angra.rj.gov.br'],
            ['instituicao' => 'Secretaria Municipal de Saúde (SSA)', 'nome' => 'Girlene Teodoro de Oliveira', 'matricula' => '4502344', 'telefone_ramal' => '2228', 'email' => 'hmj.rh@angra.rj.gov'],
            ['instituicao' => 'Secretaria Municipal de Saúde (SSA)', 'nome' => 'Jennifer Milena dos Santos Barbosa', 'matricula' => '31272', 'telefone_ramal' => '1774', 'email' => 'ses@angra.rj.gov.br'],
            ['instituicao' => 'Secretaria Municipal de Saúde (SSA)', 'nome' => 'Renata Oliveira Jordão Braga', 'matricula' => '30681', 'telefone_ramal' => '4213', 'email' => 'atprimaria@angra.rj.gov.br'],
            ['instituicao' => 'Secretaria Municipal de Saúde (SSA)', 'nome' => 'Cristiane da Silva Costa', 'matricula' => '11641', 'telefone_ramal' => '1028', 'email' => 'ssa.suger@angra.rj.gov.br'],

            // SSP
            ['instituicao' => 'Secretaria de Segurança Pública (SSP)', 'nome' => 'Anna Paula Nascimento Pereira da Silva', 'matricula' => '7034', 'telefone' => '3364-4038'],
            ['instituicao' => 'Secretaria de Segurança Pública (SSP)', 'nome' => 'Maísa Gil da Silva', 'matricula' => '30904', 'telefone' => '3364-4038', 'telefone_ramal' => '1017'],
            ['instituicao' => 'Secretaria de Segurança Pública (SSP)', 'nome' => 'Alvanir Eleutério dos Anjos', 'matricula' => '4516', 'telefone_ramal' => '1490'],
            ['instituicao' => 'Secretaria de Segurança Pública (SSP)', 'nome' => 'Rafael Fonseca e Souza', 'matricula' => '13342', 'telefone' => '3365-2792', 'telefone_ramal' => '1499'],
            ['instituicao' => 'Secretaria de Segurança Pública (SSP)', 'nome' => 'Anna Márcia Vidal de Brito Ciza', 'matricula' => '33216', 'telefone_ramal' => '1017'],

            // SUPJ
            ['instituicao' => 'Secretaria de Urbanização, Parques e Jardins (SUPJ)', 'nome' => 'Mônica Cristina de Lima Barbosa', 'matricula' => '32567', 'telefone' => '(24) 99965-3470'],
            ['instituicao' => 'Secretaria de Urbanização, Parques e Jardins (SUPJ)', 'nome' => 'Leandro Corrêa da Silva', 'matricula' => '32578', 'telefone' => '(24) 98805-1896-6522', 'telefone_ramal' => '1023'],

            // SEINF
            ['instituicao' => 'Secretaria Extraordinária de Infraestrutura (SEINF)', 'nome' => 'Zelimare Ribeiro do Nascimento Bernardo', 'matricula' => '13541', 'telefone_ramal' => '1751'],
            ['instituicao' => 'Secretaria Extraordinária de Infraestrutura (SEINF)', 'nome' => 'Carlos Jose Maia da Rocha', 'matricula' => '3731', 'telefone_ramal' => '1313'],

            // SOH
            ['instituicao' => 'Secretaria de Obras e Habitação (SOH)', 'nome' => 'Mariana de Souza Gomes', 'matricula' => '32714', 'telefone_ramal' => '1026'],
            ['instituicao' => 'Secretaria de Obras e Habitação (SOH)', 'nome' => 'Gustavo da Silva Marinho', 'matricula' => '33752', 'telefone_ramal' => '2021'],

            // CTPRE
            ['instituicao' => 'Coordenação Técnica de Pregão (CTPRE)', 'nome' => 'Lucas de Sousa Nascimento', 'matricula' => '32556', 'telefone' => '3365-6439', 'telefone_ramal' => '1158', 'email' => 'pregao05@angra.rj.gov.br'],
        ];

        $criados = 0;
        $naoEncontrados = 0;
        $instituicoesNaoEncontradas = [];

        foreach ($pontosFocais as $ponto) {
            $structure = $findStructure($ponto['instituicao']);

            if (!$structure) {
                $naoEncontrados++;
                if (!in_array($ponto['instituicao'], $instituicoesNaoEncontradas)) {
                    $instituicoesNaoEncontradas[] = $ponto['instituicao'];
                }
                $this->command->warn("⚠ Instituição não encontrada: {$ponto['instituicao']} - Ponto focal: {$ponto['nome']}");
                continue;
            }

            $telefoneRamal = '';
            if (isset($ponto['telefone'])) {
                $telefoneRamal = $ponto['telefone'];
            }
            if (isset($ponto['telefone_ramal'])) {
                if ($telefoneRamal) {
                    $telefoneRamal = $telefoneRamal . ' / Ramal: ' . $ponto['telefone_ramal'];
                } else {
                    $telefoneRamal = 'Ramal: ' . $ponto['telefone_ramal'];
                }
            }

            try {
                // Verificar se já existe um ponto focal com mesmo nome e matrícula para esta instituição
                $existe = PontoFocal::where('structure_id', $structure->id)
                    ->where('nome', $ponto['nome'])
                    ->where('matricula', $ponto['matricula'])
                    ->first();

                if ($existe) {
                    $this->command->warn("⚠ Já existe: {$ponto['nome']} - {$structure->name} ({$structure->abbreviation}) - Pulando...");
                    continue;
                }

                PontoFocal::create([
                    'id' => Str::uuid(),
                    'structure_id' => $structure->id,
                    'nome' => $ponto['nome'],
                    'matricula' => $ponto['matricula'],
                    'telefone_ramal' => $telefoneRamal ?: null,
                    'email' => $ponto['email'] ?? null,
                    'observacao' => null,
                ]);
                $criados++;
                $this->command->info("✅ Criado: {$ponto['nome']} - {$structure->name} ({$structure->abbreviation})");
            } catch (\Exception $e) {
                $this->command->error("❌ Erro ao criar ponto focal {$ponto['nome']}: " . $e->getMessage());
            }
        }

        $this->command->info("\n📊 Resumo:");
        $this->command->info("✅ Pontos focais criados: {$criados}");
        $this->command->info("⚠ Pontos focais não criados (instituição não encontrada): {$naoEncontrados}");

        if (count($instituicoesNaoEncontradas) > 0) {
            $this->command->warn("\n📋 Instituições não encontradas no banco:");
            foreach ($instituicoesNaoEncontradas as $inst) {
                $this->command->warn("   - {$inst}");
            }
            $this->command->info("\n💡 Dica: Verifique se essas instituições existem na tabela 'structures' com as siglas corretas.");
        }
    }
}


