// tools/design-schema-mobile.ts — Mobile schema generation (SPEC-013)
// Contains CoreData / Room / Realm schema generation support.
export function isMobileSchema(knowledge) {
    const fw = (knowledge.framework ?? '').toLowerCase();
    const orm = (knowledge.orm ?? '').toLowerCase();
    const db = knowledge.database.toLowerCase();
    const mobileFrameworks = ['coredata', 'room', 'realm'];
    if (mobileFrameworks.some((m) => fw === m || orm === m || db === m)) {
        return true;
    }
    return (knowledge.apps ?? []).some((a) => {
        const mf = (a.mobileFramework ?? '').toLowerCase();
        return mobileFrameworks.includes(mf);
    });
}
export function generateMobileSchema(entities, knowledge) {
    const fw = (knowledge.framework ?? '').toLowerCase();
    const orm = (knowledge.orm ?? '').toLowerCase();
    const db = knowledge.database.toLowerCase();
    const mobileFramework = knowledge.apps.map((a) => (a.mobileFramework ?? '').toLowerCase()).find((m) => m !== '') ?? '';
    const isCoreData = fw === 'coredata' || db === 'coredata' || mobileFramework === 'coredata';
    const isRoom = orm === 'room' || fw === 'room';
    const isRealm = fw === 'realm' || db === 'realm';
    const blocks = [];
    for (const entity of entities) {
        const className = toPascalCaseName(entity.name);
        const fieldComments = entity.fields.map((f) => `    // ${f}: <type>`).join('\n');
        if (isCoreData) {
            blocks.push(buildCoreDataBlock(className, fieldComments));
        }
        else if (isRoom) {
            blocks.push(buildRoomBlock(className, entity.name, fieldComments));
        }
        else if (isRealm) {
            blocks.push(buildRealmBlock(className, fieldComments));
        }
    }
    return blocks.join('\n\n---\n\n');
}
function buildCoreDataBlock(className, fieldComments) {
    return (`import CoreData\n\n` +
        `@objc(${className})\n` +
        `public class ${className}: NSManagedObject {\n` +
        `    @NSManaged public var id: UUID\n` +
        `    @NSManaged public var createdAt: Date\n` +
        `${fieldComments}\n\n` +
        `    @nonobjc public class func fetchRequest() -> NSFetchRequest<${className}> {\n` +
        `        return NSFetchRequest<${className}>(entityName: "${className}")\n` +
        `    }\n` +
        `}`);
}
function buildRoomBlock(className, entityName, fieldComments) {
    const tableName = entityName.toLowerCase();
    return (`import androidx.room.*\nimport java.util.UUID\n\n` +
        `@Entity(tableName = "${tableName}")\n` +
        `data class ${className}(\n` +
        `    @PrimaryKey val id: UUID = UUID.randomUUID(),\n` +
        `    @ColumnInfo(name = "created_at") val createdAt: Long = System.currentTimeMillis()\n` +
        `${fieldComments}\n` +
        `)\n\n` +
        `@Dao\ninterface ${className}Dao {\n` +
        `    @Query("SELECT * FROM ${tableName}") suspend fun getAll(): List<${className}>\n` +
        `    @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun insert(entity: ${className})\n` +
        `    @Update suspend fun update(entity: ${className})\n` +
        `    @Delete suspend fun delete(entity: ${className})\n` +
        `}\n\n` +
        `@Database(entities = [${className}::class], version = 1)\n` +
        `abstract class AppDatabase : RoomDatabase() {\n` +
        `    abstract fun ${entityName.toLowerCase()}Dao(): ${className}Dao\n` +
        `}`);
}
function buildRealmBlock(className, fieldComments) {
    return (`import io.realm.kotlin.types.RealmObject\n` +
        `import io.realm.kotlin.types.annotations.PrimaryKey\n` +
        `import org.mongodb.bson.BsonObjectId\n\n` +
        `class ${className} : RealmObject {\n` +
        `    @PrimaryKey var _id: BsonObjectId = BsonObjectId()\n` +
        `    var createdAt: Long = System.currentTimeMillis()\n` +
        `${fieldComments}\n` +
        `}`);
}
export function toPascalCaseName(name) {
    return name
        .split(/[_\s-]+/)
        .map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase())
        .join('');
}
//# sourceMappingURL=design-schema-mobile.js.map