/*
 * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). All Rights Reserved.
 *
 * This software is the property of WSO2 LLC. and its suppliers, if any.
 * Dissemination of any information or reproduction of any material contained
 * herein in any form is strictly forbidden, unless permitted by WSO2 expressly.
 * You may not alter or remove any copyright or other notice from copies of this content.
 */

package org.wso2.carbon.is.migration.service.v700.model;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.UUID;

import static org.wso2.carbon.is.migration.service.v700.util.Utils.formatTimestamp;

/**
 * Group model.
 */
public class Group {

    private String roleName;
    private int tenantID;
    private String roleUUID;
    private Timestamp created;
    private Timestamp modified;
    private final String UUID_URI = "urn:ietf:params:scim:schemas:core:2.0:id";
    private final String CREATED_URI = "urn:ietf:params:scim:schemas:core:2.0:meta.created";
    private final String MODIFIED_URI = "urn:ietf:params:scim:schemas:core:2.0:meta.lastModified";

    public Group(String roleName, int tenantID) {

        this.roleName = roleName;
        this.tenantID = tenantID;
    }

    public int getTenantID() {

        return tenantID;
    }

    public void setTenantID(int tenantID) {

        this.tenantID = tenantID;
    }

    public String getRoleName() {

        return this.roleName;
    }

    public void setRoleName(String roleName) {

        this.roleName = roleName;
    }

    public String getRoleUUID() {

        return this.roleUUID;
    }

    public void setRoleUUID(String roleUUID) {

        this.roleUUID = roleUUID;
    }

    public Timestamp getCreated() {

        return this.created;
    }

    public void setCreated(Timestamp created) {

        this.created = created;
    }

    public void setCreated(String created) {

        setCreated(formatTimestamp(created));
    }

    public Timestamp getModified() {

        return modified;
    }

    public void setModified(Timestamp modified) {

        this.modified = modified;
    }

    public void setModified(String modified) {

        setModified(formatTimestamp(modified));
    }

    /**
     * Updating the attribute value by attribute name.
     *
     * @param attributeName  Attribute name.
     * @param attributeValue Attribute value.
     */
    public void setAttribute(String attributeName, String attributeValue) {

        switch (attributeName) {
            case UUID_URI:
                setRoleUUID(attributeValue);
                break;
            case CREATED_URI:
                setCreated(attributeValue);
                break;
            case MODIFIED_URI:
                setModified(attributeValue);
                break;
        }
    }

    /**
     * Set the default values to attributes if they don't have a value.
     */
    public void setDefaultIfNull() {

        if (getRoleUUID() == null) {
            setRoleUUID(UUID.randomUUID().toString());
        }

        if (getCreated() == null) {
            setCreated(Timestamp.from(Instant.now()));
        }

        if (getModified() == null) {
            setModified(Timestamp.from(Instant.now()));
        }
    }
}
