import { app } from 'electron';
import { readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';

export interface WindowState {
  width: number;
  height: number;
  x?: number;
  y?: number;
  maximized?: boolean;
}

const DEFAULT: WindowState = { width: 1400, height: 900 };

function stateFile(): string {
  return join(app.getPath('userData'), 'window-state.json');
}

export function loadWindowState(): WindowState {
  try {
    const parsed = JSON.parse(readFileSync(stateFile(), 'utf8')) as Partial<WindowState>;
    return { ...DEFAULT, ...parsed };
  } catch {
    return { ...DEFAULT };
  }
}

export function saveWindowState(state: WindowState): void {
  try {
    writeFileSync(stateFile(), JSON.stringify(state));
  } catch {
    /* best effort : quota / permissions */
  }
}
