// Copyright © 2025 The µcad authors <info@ucad.xyz>
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Processed Model Info, generated by [`Processor`].

use bevy::{asset::uuid::Uuid, color::Color, reflect::TypePath, transform::components::Transform};
use microcad_core::*;
use microcad_lang::{
    model::{Model, OutputType},
    render::ComputedHash,
    src_ref::{SrcRef, SrcReferrer},
};

use crate::{ToBevy, processor::generate_model_geometry_output_uuid};

#[derive(Clone, Default, TypePath)]
pub struct ModelInfo {
    /// The uuid of the model.
    pub model_uuid: Uuid,
    pub geometry_output_uuid: Uuid,
    pub model_hash: u64,
    pub output_type: OutputType,
    pub transform: Transform,
    pub color: Option<Color>,
    pub bounds_2d: Bounds2D,
    pub bounds_3d: Bounds3D,
    pub scene_radius: Length,
    pub ground_radius: Length,
    pub src_ref: SrcRef,
}

impl ModelInfo {
    pub fn from_model(model: &Model) -> Self {
        let model_ = model.borrow();
        let output = model_.output();

        Self {
            model_uuid: super::generate_model_uuid(model),
            model_hash: model.computed_hash(),
            geometry_output_uuid: generate_model_geometry_output_uuid(model),
            output_type: model.render_output_type(),
            transform: output.world_matrix.expect("Some matrix").to_bevy(),
            color: output.attributes.get_color().map(|c| c.to_bevy()),
            bounds_2d: output.calc_bounds_2d(),
            bounds_3d: output.calc_bounds_3d(),
            scene_radius: output.scene_radius(),
            ground_radius: output.ground_radius(),
            src_ref: model.src_ref(),
        }
    }
}
