// File: src/cpm.js

import { createInitialState } from './core/state.js';
import { createEventBus } from './core/events.js';
import { createRegistry } from './core/registry.js';
import { defaultConfig } from './core/config.js';
import { initCore } from './core/init.js';

// data modules
import { loadProjectCore } from './data/project.js';
import { loadTasks } from './data/task.js';
import { loadDependencies } from './data/dependency.js';
import { loadCalendars } from './data/calendar.js';
import { loadWbs } from './data/wbs.js';

import { loadResourcesAndFinancial } from './resources/index.js';

import { createUi } from './ui/index.js';


export class Cpm {
    constructor(container, options = {}) {
        if (!container) {
            throw new Error('CPM: container element is required');
        }

        this.container = container;
        this.config = {
            ...defaultConfig,
            ...options
        };

        this.events = createEventBus();
        this.state = createInitialState();
        this.registry = createRegistry();

        // Core init برای کارهای هماهنگی (در صورت نیاز)
        initCore(this);

        this._initContainer();

        createUi(this);

    }

    _initContainer() {
        this.container.innerHTML = '';

        const root = document.createElement('div');
        root.className = 'cpm-root';
        this.container.appendChild(root);

        this.rootElement = root;

        this.events.emit('cpm:init', { cpm: this });
    }

    /**
     * ورودی: یک object شبیه:
     * {
     *   project: { ... },
     *   calendars: [ ... ],
     *   wbs: [ ... ],
     *   tasks: [ ... ],
     *   dependencies: [ ... ],
     *   resources: [ ... ],
     *   assignments: [ ... ],
     *   financial_items: [ ... ],
     *   task_costs: [ ... ],
     *   cashflow: [ ... ]
     * }
     */
    loadProjectData(projectData) {
        if (!projectData || typeof projectData !== 'object') {
            console.warn('CPM.loadProjectData: invalid projectData');
            return;
        }

        this._resetState();

        loadProjectCore(this.state, projectData);
        loadCalendars(this.state, projectData);
        loadWbs(this.state, projectData);
        loadTasks(this.state, projectData);
        loadDependencies(this.state, projectData);

        loadResourcesAndFinancial(this.state, projectData);

        this.events.emit('data:projectLoaded', {
            projectId: this.state.project.id,
            state: this.state
        });

        this.events.emit('ui:refresh', {
            reason: 'projectLoaded',
            projectId: this.state.project.id
        });
    }


    updateConfig(newConfig = {}) {
        this.config = {
            ...this.config,
            ...newConfig
        };
        this.events.emit('config:change', { config: this.config });
    }

    _resetState() {
        // از تابع کمکی core/init استفاده می‌کنیم
        if (typeof this.state._reset === 'function') {
            this.state._reset();
        } else {
            // اگر هنوز _reset را پیاده نکردیم، ساده‌ترین reset:
            Object.assign(this.state, createInitialState());
        }
        this.events.emit('state:reset', { state: this.state });
    }

    destroy() {
        this.events.emit('cpm:destroy', { cpm: this });

        if (this.rootElement && this.rootElement.parentNode) {
            this.rootElement.parentNode.removeChild(this.rootElement);
        }

        this.rootElement = null;
        this.container = null;

        this.events.clearAllListeners();
        this.state = null;
    }


}
