import { useRef, useState } from "react";
import { emptySettings, emptyTaskForm } from "../appDefaults.js";

export default function useAppDataState() {
  const [settings, setSettings] = useState(emptySettings);
  const [accounts, setAccounts] = useState([]);
  const [accountStats, setAccountStats] = useState([]);
  const [accountAssignments, setAccountAssignments] = useState([]);
  const [globalStatus, setGlobalStatus] = useState({ connectedSessions: 0, totalAccounts: 0 });
  const [logs, setLogs] = useState([]);
  const [invites, setInvites] = useState([]);
  const [fallbackList, setFallbackList] = useState([]);
  const [confirmQueue, setConfirmQueue] = useState([]);
  const [tasks, setTasks] = useState([]);
  const [selectedTaskId, setSelectedTaskId] = useState(null);
  const [taskForm, setTaskForm] = useState(emptyTaskForm);
  const [competitorText, setCompetitorText] = useState("");
  const [selectedAccountIds, setSelectedAccountIds] = useState([]);
  const [taskAccountRoles, setTaskAccountRoles] = useState({});
  const [activePreset, setActivePreset] = useState("");
  const presetSignatureRef = useRef("");
  const [taskStatus, setTaskStatus] = useState({
    running: false,
    queueCount: 0,
    dailyRemaining: 0,
    dailyUsed: 0,
    dailyLimit: 0,
    cycleCompetitors: false,
    competitorCursor: 0,
    monitorInfo: { monitoring: false, accountId: 0, accountIds: [], groups: [], lastMessageAt: "", lastSource: "" },
    nextRunAt: "",
    nextInviteAccountId: 0,
    lastInviteAccountId: 0,
    pendingStats: { total: 0, withUsername: 0, withAccessHash: 0, withoutData: 0 },
    warnings: [],
    lastStopReason: "",
    lastStopAt: ""
  });
  const [taskStatusMap, setTaskStatusMap] = useState({});
  const [membershipStatus, setMembershipStatus] = useState({});
  const [groupVisibility, setGroupVisibility] = useState([]);
  const [accessStatus, setAccessStatus] = useState([]);
  const [inviteAccessStatus, setInviteAccessStatus] = useState([]);
  const [inviteAccessCheckedAt, setInviteAccessCheckedAt] = useState("");
  const [accountEvents, setAccountEvents] = useState([]);
  const [taskAudit, setTaskAudit] = useState([]);
  const [testRun, setTestRun] = useState({
    status: "idle",
    mode: "safe",
    steps: [],
    startedAt: "",
    finishedAt: "",
    summary: ""
  });
  const [queueItems, setQueueItems] = useState([]);
  const [queueStats, setQueueStats] = useState({ total: 0, withUsername: 0, withAccessHash: 0, withoutData: 0 });
  const [fileImportForm, setFileImportForm] = useState({
    onlyIds: false,
    sourceChat: ""
  });
  const [fileImportResult, setFileImportResult] = useState(null);
  const [taskActionLoading, setTaskActionLoading] = useState(false);
  const [taskNotice, setTaskNotice] = useState(null);
  const [autosaveNote, setAutosaveNote] = useState("");
  const [settingsNotice, setSettingsNotice] = useState(null);
  const [tdataNotice, setTdataNotice] = useState(null);

  return {
    settings,
    setSettings,
    accounts,
    setAccounts,
    accountStats,
    setAccountStats,
    accountAssignments,
    setAccountAssignments,
    globalStatus,
    setGlobalStatus,
    logs,
    setLogs,
    invites,
    setInvites,
    fallbackList,
    setFallbackList,
    confirmQueue,
    setConfirmQueue,
    tasks,
    setTasks,
    selectedTaskId,
    setSelectedTaskId,
    taskForm,
    setTaskForm,
    competitorText,
    setCompetitorText,
    selectedAccountIds,
    setSelectedAccountIds,
    taskAccountRoles,
    setTaskAccountRoles,
    activePreset,
    setActivePreset,
    presetSignatureRef,
    taskStatus,
    setTaskStatus,
    taskStatusMap,
    setTaskStatusMap,
    membershipStatus,
    setMembershipStatus,
    groupVisibility,
    setGroupVisibility,
    accessStatus,
    setAccessStatus,
    inviteAccessStatus,
    setInviteAccessStatus,
    inviteAccessCheckedAt,
    setInviteAccessCheckedAt,
    accountEvents,
    setAccountEvents,
    taskAudit,
    setTaskAudit,
    testRun,
    setTestRun,
    queueItems,
    setQueueItems,
    queueStats,
    setQueueStats,
    fileImportForm,
    setFileImportForm,
    fileImportResult,
    setFileImportResult,
    taskActionLoading,
    setTaskActionLoading,
    taskNotice,
    setTaskNotice,
    autosaveNote,
    setAutosaveNote,
    settingsNotice,
    setSettingsNotice,
    tdataNotice,
    setTdataNotice
  };
}
