use std::fs;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::aggregate::fs::balance_sheet::ConsolidatedBalanceSheet;
use crate::aggregate::fs::cash_flow::ConsolidatedCashFlow;
use crate::aggregate::fs::consolidation_schedule::ConsolidationSchedule;
use crate::aggregate::fs::equity_changes::StatementOfChangesInEquity;
use crate::aggregate::fs::income_statement::ConsolidatedIncomeStatement;
use crate::aggregate::fs::notes::NotesToConsolidatedFs;
use crate::errors::{GroupError, GroupResult};
pub const CONSOLIDATED_SUBDIR: &str = "consolidated";
pub const CONSOLIDATED_FS_FILENAME: &str = "consolidated_financial_statements.json";
pub const CONSOLIDATION_SCHEDULE_FILENAME: &str = "consolidation_schedule.json";
pub const NOTES_FILENAME: &str = "notes_to_consolidated_fs.json";
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ConsolidatedFinancialStatements {
pub balance_sheet: ConsolidatedBalanceSheet,
pub income_statement: ConsolidatedIncomeStatement,
pub cash_flow: ConsolidatedCashFlow,
pub changes_in_equity: StatementOfChangesInEquity,
}
pub fn write_consolidated_fs(
fs_bundle: &ConsolidatedFinancialStatements,
schedule: &ConsolidationSchedule,
notes: &NotesToConsolidatedFs,
out_dir: &Path,
) -> GroupResult<Vec<PathBuf>> {
let dir = out_dir.join(CONSOLIDATED_SUBDIR);
fs::create_dir_all(&dir).map_err(GroupError::Io)?;
let fs_path = dir.join(CONSOLIDATED_FS_FILENAME);
let mut fs_json = serde_json::to_string_pretty(fs_bundle)?;
fs_json.push('\n');
fs::write(&fs_path, fs_json).map_err(GroupError::Io)?;
let sched_path = dir.join(CONSOLIDATION_SCHEDULE_FILENAME);
let mut sched_json = serde_json::to_string_pretty(schedule)?;
sched_json.push('\n');
fs::write(&sched_path, sched_json).map_err(GroupError::Io)?;
let notes_path = dir.join(NOTES_FILENAME);
let mut notes_json = serde_json::to_string_pretty(notes)?;
notes_json.push('\n');
fs::write(¬es_path, notes_json).map_err(GroupError::Io)?;
Ok(vec![fs_path, sched_path, notes_path])
}