import React, { createContext, useContext, useState, ReactNode } from "react";
import { EventData, EventStatus, EventType, Institution } from "../types";

// Initial Mock Data
const INITIAL_INSTITUTIONS: Institution[] = [
  {
    id: "inst-1",
    name: "Universidade Federal do Rio Grande do Sul",
    type: "Universidade Pública",
    phone: "(51) 3308-3333",
    email: "eventos@ufrgs.br",
    address: {
      street: "Av. Paulo Gama",
      number: "110",
      city: "Porto Alegre",
      state: "RS",
      zip: "90040-060",
    },
    description:
      "Campus Central - Principais eventos realizados no Salão de Atos",
    ownerId: "client-1",
  },
];

const INITIAL_EVENTS: EventData[] = [
  {
    id: "1",
    name: "Formatura Engenharia Civil",
    date: "2025-12-05",
    time: "19:00",
    type: EventType.GRADUATION,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Av. das Hortênsias",
      number: "1200",
      city: "Gramado",
      state: "RS",
      zip: "95670-000",
    },
    briefing: "Cerimônia de formatura com 120 formandos. Foco em fotos individuais e da turma.",
    coverImage: "https://picsum.photos/id/1059/800/400",
    contacts: [
      {
        id: "c1",
        name: "Comissão de Formatura",
        role: "Organizador",
        phone: "51 99999-1111",
        email: "formatura@email.com",
      },
    ],
    checklist: [],
    ownerId: "client-1",
    photographerIds: ["photographer-1", "photographer-2"],
    institutionId: "inst-1",
  },
  {
    id: "2",
    name: "Colação de Grau Medicina",
    date: "2025-12-05",
    time: "10:00",
    type: EventType.COLATION,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Rua Olimpíadas",
      number: "205",
      city: "São Paulo",
      state: "SP",
      zip: "04551-000",
    },
    briefing: "Colação de grau solene. Capturar juramento e entrega de diplomas.",
    coverImage: "https://picsum.photos/id/3/800/400",
    contacts: [
      {
        id: "c2",
        name: "Secretaria Acadêmica",
        role: "Coordenador",
        phone: "11 98888-2222",
        email: "academico@med.br",
      },
    ],
    checklist: [],
    ownerId: "client-1",
    photographerIds: ["photographer-1"],
  },
  {
    id: "3",
    name: "Semana Acadêmica Direito",
    date: "2025-12-05",
    time: "14:00",
    type: EventType.ACADEMIC_WEEK,
    status: EventStatus.IN_PROGRESS,
    address: {
      street: "Av. Paulista",
      number: "1500",
      city: "São Paulo",
      state: "SP",
      zip: "01310-100",
    },
    briefing: "Palestras e painéis durante toda a semana. Cobertura de 3 dias.",
    coverImage: "https://picsum.photos/id/10/800/400",
    contacts: [],
    checklist: [],
    ownerId: "client-2",
    photographerIds: ["photographer-2"],
  },
  {
    id: "4",
    name: "Defesa de Doutorado - Maria Silva",
    date: "2025-12-05",
    time: "15:30",
    type: EventType.DEFENSE,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Rua Ramiro Barcelos",
      number: "2600",
      city: "Porto Alegre",
      state: "RS",
      zip: "90035-003",
    },
    briefing: "Defesa de tese em sala fechada. Fotos discretas da apresentação e banca.",
    coverImage: "https://picsum.photos/id/20/800/400",
    contacts: [
      {
        id: "c3",
        name: "Prof. João Santos",
        role: "Orientador",
        phone: "51 97777-3333",
        email: "joao@univ.br",
      },
    ],
    checklist: [],
    ownerId: "client-1",
    photographerIds: ["photographer-1"],
  },
  {
    id: "5",
    name: "Semana de Calouros 2026",
    date: "2025-12-06",
    time: "09:00",
    type: EventType.FRESHMAN_WEEK,
    status: EventStatus.PENDING_APPROVAL,
    address: {
      street: "Campus Universitário",
      number: "s/n",
      city: "Curitiba",
      state: "PR",
      zip: "80060-000",
    },
    briefing: "Recepção dos calouros com atividades de integração e gincanas.",
    coverImage: "https://picsum.photos/id/30/800/400",
    contacts: [],
    checklist: [],
    ownerId: "client-2",
    photographerIds: [],
  },
  {
    id: "6",
    name: "Formatura Administração",
    date: "2025-12-06",
    time: "20:00",
    type: EventType.GRADUATION,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Av. Ipiranga",
      number: "6681",
      city: "Porto Alegre",
      state: "RS",
      zip: "90619-900",
    },
    briefing: "Formatura noturna com jantar. Fotos da cerimônia e festa.",
    coverImage: "https://picsum.photos/id/40/800/400",
    contacts: [
      {
        id: "c4",
        name: "Lucas Oliveira",
        role: "Presidente da Comissão",
        phone: "51 96666-4444",
        email: "lucas@formatura.com",
      },
    ],
    checklist: [],
    ownerId: "client-1",
    photographerIds: ["photographer-2"],
  },
  {
    id: "7",
    name: "Congresso de Tecnologia",
    date: "2025-12-06",
    time: "08:30",
    type: EventType.SYMPOSIUM,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Av. das Nações Unidas",
      number: "12901",
      city: "São Paulo",
      state: "SP",
      zip: "04578-000",
    },
    briefing: "Congresso com múltiplas salas. Cobrir palestrantes principais e stands.",
    coverImage: "https://picsum.photos/id/50/800/400",
    contacts: [
      {
        id: "c5",
        name: "Eventos Tech",
        role: "Organizadora",
        phone: "11 95555-5555",
        email: "contato@eventostech.com",
      },
    ],
    checklist: [],
    ownerId: "client-2",
    photographerIds: ["photographer-1", "photographer-3"],
  },
  {
    id: "8",
    name: "Campeonato Universitário de Futsal",
    date: "2025-12-06",
    time: "16:00",
    type: EventType.SPORTS_EVENT,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Rua dos Esportes",
      number: "500",
      city: "Gramado",
      state: "RS",
      zip: "95670-100",
    },
    briefing: "Final do campeonato. Fotos dinâmicas da partida e premiação.",
    coverImage: "https://picsum.photos/id/60/800/400",
    contacts: [],
    checklist: [],
    ownerId: "client-1",
    photographerIds: ["photographer-3"],
  },
  {
    id: "9",
    name: "Colação de Grau Odontologia",
    date: "2025-12-07",
    time: "11:00",
    type: EventType.COLATION,
    status: EventStatus.PLANNING,
    address: {
      street: "Rua Voluntários da Pátria",
      number: "89",
      city: "Porto Alegre",
      state: "RS",
      zip: "90230-010",
    },
    briefing: "Cerimônia formal de colação. Fotos individuais e em grupo.",
    coverImage: "https://picsum.photos/id/70/800/400",
    contacts: [
      {
        id: "c6",
        name: "Direção da Faculdade",
        role: "Coordenador",
        phone: "51 94444-6666",
        email: "direcao@odonto.edu",
      },
    ],
    checklist: [],
    ownerId: "client-1",
    photographerIds: ["photographer-2"],
  },
  {
    id: "10",
    name: "Festival Cultural Universitário",
    date: "2025-12-07",
    time: "18:00",
    type: EventType.CULTURAL_EVENT,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Praça da República",
      number: "s/n",
      city: "São Paulo",
      state: "SP",
      zip: "01045-000",
    },
    briefing: "Festival com apresentações musicais e teatrais. Cobertura completa.",
    coverImage: "https://picsum.photos/id/80/800/400",
    contacts: [],
    checklist: [],
    ownerId: "client-2",
    photographerIds: ["photographer-1"],
  },
  {
    id: "11",
    name: "Defesa de Mestrado - Pedro Costa",
    date: "2025-12-07",
    time: "14:00",
    type: EventType.DEFENSE,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Av. Bento Gonçalves",
      number: "9500",
      city: "Porto Alegre",
      state: "RS",
      zip: "91509-900",
    },
    briefing: "Defesa de dissertação. Registro da apresentação e momento da aprovação.",
    coverImage: "https://picsum.photos/id/90/800/400",
    contacts: [],
    checklist: [],
    ownerId: "client-1",
    photographerIds: ["photographer-3"],
  },
  {
    id: "12",
    name: "Formatura Psicologia",
    date: "2025-12-08",
    time: "19:30",
    type: EventType.GRADUATION,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Av. Protásio Alves",
      number: "7000",
      city: "Porto Alegre",
      state: "RS",
      zip: "91310-000",
    },
    briefing: "Formatura emotiva com homenagens. Foco em momentos especiais.",
    coverImage: "https://picsum.photos/id/100/800/400",
    contacts: [
      {
        id: "c7",
        name: "Ana Paula",
        role: "Formanda",
        phone: "51 93333-7777",
        email: "ana@email.com",
      },
    ],
    checklist: [],
    ownerId: "client-1",
    photographerIds: ["photographer-1", "photographer-2"],
  },
  {
    id: "13",
    name: "Simpósio de Engenharia",
    date: "2025-12-08",
    time: "09:00",
    type: EventType.SYMPOSIUM,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Av. Sertório",
      number: "6600",
      city: "Porto Alegre",
      state: "RS",
      zip: "91040-000",
    },
    briefing: "Apresentações técnicas e workshops. Cobrir painéis principais.",
    coverImage: "https://picsum.photos/id/110/800/400",
    contacts: [],
    checklist: [],
    ownerId: "client-1",
    photographerIds: ["photographer-2"],
  },
  {
    id: "14",
    name: "Torneio de Vôlei Universitário",
    date: "2025-12-08",
    time: "15:00",
    type: EventType.SPORTS_EVENT,
    status: EventStatus.IN_PROGRESS,
    address: {
      street: "Rua Faria Santos",
      number: "100",
      city: "Curitiba",
      state: "PR",
      zip: "80060-150",
    },
    briefing: "Semifinais e final. Fotos de ação e torcida.",
    coverImage: "https://picsum.photos/id/120/800/400",
    contacts: [],
    checklist: [],
    ownerId: "client-2",
    photographerIds: ["photographer-3"],
  },
  {
    id: "15",
    name: "Colação de Grau Enfermagem",
    date: "2025-12-09",
    time: "10:30",
    type: EventType.COLATION,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Rua São Manoel",
      number: "963",
      city: "São Paulo",
      state: "SP",
      zip: "01330-001",
    },
    briefing: "Colação com juramento de Florence Nightingale. Momento solene.",
    coverImage: "https://picsum.photos/id/130/800/400",
    contacts: [
      {
        id: "c8",
        name: "Coordenação de Enfermagem",
        role: "Coordenador",
        phone: "11 92222-8888",
        email: "coord@enf.br",
      },
    ],
    checklist: [],
    ownerId: "client-2",
    photographerIds: ["photographer-1"],
  },
  {
    id: "16",
    name: "Semana Acadêmica Biomedicina",
    date: "2025-12-09",
    time: "13:00",
    type: EventType.ACADEMIC_WEEK,
    status: EventStatus.PLANNING,
    address: {
      street: "Av. Independência",
      number: "2293",
      city: "Porto Alegre",
      state: "RS",
      zip: "90035-075",
    },
    briefing: "Palestras e atividades práticas. Cobertura de 2 dias.",
    coverImage: "https://picsum.photos/id/140/800/400",
    contacts: [],
    checklist: [],
    ownerId: "client-1",
    photographerIds: [],
  },
  {
    id: "17",
    name: "Formatura Ciências Contábeis",
    date: "2025-12-09",
    time: "20:30",
    type: EventType.GRADUATION,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Av. das Américas",
      number: "3500",
      city: "Gramado",
      state: "RS",
      zip: "95670-200",
    },
    briefing: "Formatura elegante em hotel. Cobertura completa da cerimônia e recepção.",
    coverImage: "https://picsum.photos/id/150/800/400",
    contacts: [
      {
        id: "c9",
        name: "Rodrigo Almeida",
        role: "Tesoureiro",
        phone: "51 91111-9999",
        email: "rodrigo@turma.com",
      },
    ],
    checklist: [],
    ownerId: "client-1",
    photographerIds: ["photographer-2", "photographer-3"],
  },
  {
    id: "18",
    name: "Defesa de TCC - Turma 2025",
    date: "2025-12-09",
    time: "16:30",
    type: EventType.DEFENSE,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Rua Marquês do Pombal",
      number: "2000",
      city: "Porto Alegre",
      state: "RS",
      zip: "90540-000",
    },
    briefing: "Múltiplas defesas sequenciais. Fotos rápidas de cada apresentação.",
    coverImage: "https://picsum.photos/id/160/800/400",
    contacts: [],
    checklist: [],
    ownerId: "client-1",
    photographerIds: ["photographer-1"],
  },
  {
    id: "19",
    name: "Festival de Música Universitária",
    date: "2025-12-10",
    time: "19:00",
    type: EventType.CULTURAL_EVENT,
    status: EventStatus.PENDING_APPROVAL,
    address: {
      street: "Parque da Redenção",
      number: "s/n",
      city: "Porto Alegre",
      state: "RS",
      zip: "90040-000",
    },
    briefing: "Festival ao ar livre com várias bandas. Fotos de palco e público.",
    coverImage: "https://picsum.photos/id/170/800/400",
    contacts: [],
    checklist: [],
    ownerId: "client-2",
    photographerIds: [],
  },
  {
    id: "20",
    name: "Colação de Grau Arquitetura",
    date: "2025-12-10",
    time: "11:30",
    type: EventType.COLATION,
    status: EventStatus.CONFIRMED,
    address: {
      street: "Av. Borges de Medeiros",
      number: "1501",
      city: "Gramado",
      state: "RS",
      zip: "95670-300",
    },
    briefing: "Cerimônia especial com exposição de projetos. Fotos criativas.",
    coverImage: "https://picsum.photos/id/180/800/400",
    contacts: [
      {
        id: "c10",
        name: "Atelier Arquitetura",
        role: "Escritório Parceiro",
        phone: "51 90000-1010",
        email: "contato@atelier.arq",
      },
    ],
    checklist: [],
    ownerId: "client-1",
    photographerIds: ["photographer-3"],
  },
];

interface DataContextType {
  events: EventData[];
  institutions: Institution[];
  addEvent: (event: EventData) => void;
  updateEventStatus: (id: string, status: EventStatus) => void;
  assignPhotographer: (eventId: string, photographerId: string) => void;
  getEventsByRole: (userId: string, role: string) => EventData[];
  addInstitution: (institution: Institution) => void;
  updateInstitution: (id: string, institution: Partial<Institution>) => void;
  getInstitutionsByUserId: (userId: string) => Institution[];
  getInstitutionById: (id: string) => Institution | undefined;
}

const DataContext = createContext<DataContextType | undefined>(undefined);

export const DataProvider: React.FC<{ children: ReactNode }> = ({
  children,
}) => {
  const [events, setEvents] = useState<EventData[]>(INITIAL_EVENTS);
  const [institutions, setInstitutions] =
    useState<Institution[]>(INITIAL_INSTITUTIONS);

  const addEvent = (event: EventData) => {
    setEvents((prev) => [event, ...prev]);
  };

  const updateEventStatus = (id: string, status: EventStatus) => {
    setEvents((prev) => prev.map((e) => (e.id === id ? { ...e, status } : e)));
  };

  const assignPhotographer = (eventId: string, photographerId: string) => {
    setEvents((prev) =>
      prev.map((e) => {
        if (e.id === eventId) {
          const current = e.photographerIds || [];
          if (!current.includes(photographerId)) {
            return { ...e, photographerIds: [...current, photographerId] };
          }
        }
        return e;
      })
    );
  };

  const getEventsByRole = (userId: string, role: string) => {
    if (role === "SUPERADMIN" || role === "BUSINESS_OWNER") {
      return events;
    }
    if (role === "EVENT_OWNER") {
      return events.filter((e) => e.ownerId === userId);
    }
    if (role === "PHOTOGRAPHER") {
      return events.filter((e) => e.photographerIds.includes(userId));
    }
    return [];
  };

  const addInstitution = (institution: Institution) => {
    setInstitutions((prev) => [...prev, institution]);
  };

  const updateInstitution = (id: string, updatedData: Partial<Institution>) => {
    setInstitutions((prev) =>
      prev.map((inst) => (inst.id === id ? { ...inst, ...updatedData } : inst))
    );
  };

  const getInstitutionsByUserId = (userId: string) => {
    return institutions.filter((inst) => inst.ownerId === userId);
  };

  const getInstitutionById = (id: string) => {
    return institutions.find((inst) => inst.id === id);
  };

  return (
    <DataContext.Provider
      value={{
        events,
        institutions,
        addEvent,
        updateEventStatus,
        assignPhotographer,
        getEventsByRole,
        addInstitution,
        updateInstitution,
        getInstitutionsByUserId,
        getInstitutionById,
      }}
    >
      {children}
    </DataContext.Provider>
  );
};

export const useData = () => {
  const context = useContext(DataContext);
  if (!context) throw new Error("useData must be used within a DataProvider");
  return context;
};
