<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.config.dbmigrations;

import <%= packageName %>.domain.Authority;
import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.security.AuthoritiesConstants;
import <%= packageName %>.domain.ViewPermission;
import <%= packageName %>.config.Constants;

import io.mongock.api.annotations.ChangeUnit;
import io.mongock.api.annotations.Execution;
import io.mongock.api.annotations.RollbackExecution;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
 * Creates the initial database setup.
 */
@ChangeUnit(id="users-initialization", order = "001")
public class InitialSetupMigration {

    private final MongoTemplate template;

    public InitialSetupMigration(MongoTemplate template) {
        this.template = template;
    }

    @Execution
    public void changeSet() {
        Authority userAuthority = createUserAuthority();
        userAuthority = template.save(userAuthority);
        Authority adminAuthority = createAdminAuthority();
        adminAuthority = template.save(adminAuthority);
<%_ if (generateUserManagement) { _%>
        addUsers(userAuthority, adminAuthority);
<%_ } _%>
    }

    @RollbackExecution
    public void rollback() {
    }

    private Authority createAuthority(String authority, String id) {
        Authority adminAuthority = new Authority();
        adminAuthority.setName(authority);
        adminAuthority.setId(id);
        return adminAuthority;
    }

    private Authority createAdminAuthority() {
        Authority adminAuthority = createAuthority(AuthoritiesConstants.ADMIN, "1");
        return adminAuthority;
    }

    private Authority createUserAuthority() {
        Authority userAuthority = createAuthority(AuthoritiesConstants.USER, "2");
        return userAuthority;
    }
<%_ if (generateUserManagement) { _%>

    private void addUsers(Authority userAuthority, Authority adminAuthority) {
        <%= user.persistClass %> user = createUser(userAuthority);
        template.save(user);
        <%= user.persistClass %> admin = createAdmin(adminAuthority, userAuthority);
        template.save(admin);
    }

    private <%= user.persistClass %> createUser(Authority userAuthority) {
        <%= user.persistClass %> userUser = new <%= user.persistClass %>();
        userUser.setId("user-2");
        userUser.setLogin("user");
        userUser.setPassword("$2a$10$VEjxo0jq2YG9Rbk2HmX9S.k1uZBGYUHdUcid3g/vfiEl7lwWgOH/K");
        userUser.setFirstName("User");
        userUser.setLastName("User");
        userUser.setEmail("user@localhost");
        userUser.setActivated(true);
        userUser.setLangKey("<%= nativeLanguage %>");
        userUser.setCreatedBy(Constants.SYSTEM);
        userUser.setCreatedDate(Instant.now());
        userUser.getAuthorities().add(userAuthority);
        return userUser;
    }

    private <%= user.persistClass %> createAdmin(Authority adminAuthority, Authority userAuthority) {
        <%= user.persistClass %> adminUser = new <%= user.persistClass %>();
        adminUser.setId("user-1");
        adminUser.setLogin("admin");
        adminUser.setPassword("$2a$10$gSAhZrxMllrbgj/kkK9UceBPpChGWJA7SYIb1Mqo.n5aNLq1/oRrC");
        adminUser.setFirstName("admin");
        adminUser.setLastName("Administrator");
        adminUser.setEmail("admin@localhost");
        adminUser.setActivated(true);
        adminUser.setLangKey("<%= nativeLanguage %>");
        adminUser.setCreatedBy(Constants.SYSTEM);
        adminUser.setCreatedDate(Instant.now());
        adminUser.getAuthorities().add(adminAuthority);
        adminUser.getAuthorities().add(userAuthority);
        adminUser.setImageUrl("/avatar/avatar02.jpg");
        return adminUser;
    }
<%_ } _%>
}
