package com.aip.gateway.api.repository;

import com.aip.gateway.api.aip.manager.AipFileManager;
import com.aip.gateway.api.aip.model.AipLabel;
import com.aip.gateway.api.aip.model.AipTemplate;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Getter
@Component
@RequiredArgsConstructor
public class AipFileRepository {

    private final AppRepository repo;
    private AipFileManager manager;

    public boolean initialize() {
        this.manager = new AipFileManager(this.repo.getSettings().getConfig());
        if (!this.manager.initialize()) {
            log.error("AipFileManager.initialize Failed, {}, {}", this.manager.getLastErrNo(), this.manager.getLastErrMsg());
            return false;
        }
        if (!this.manager.createProfile()) {
            log.error("AipFileManager.createProfile Failed, {}, {}", this.manager.getLastErrNo(), this.manager.getLastErrMsg());
            return false;
        }
        if (!this.manager.createEngine()) {
            log.error("AipFileManager.createEngine Failed, {}, {}", this.manager.getLastErrNo(), this.manager.getLastErrMsg());
            return false;
        }
        log.info("AipFileManager Initialize And Create Success.");

        List<AipLabel> labels = this.manager.sensitivityLabels();
        log.info("{}", labels);
        List<AipLabel> policies = this.manager.listSensitivityLabels();
        log.info("{}", policies);
        List<AipTemplate> templates = this.manager.getTemplates();
        log.info("{}", templates);

        return true;
    }
}
