package com.utic.center.utic.stat.server.service;

import com.utic.center.common.annotation.ProcessingElapsed;
import com.utic.center.common.service.AbstractProcessService;
import com.utic.center.utic.stat.server.dao.repository.dwdb.UticStatTableManagerRepository;
import com.utic.center.utic.stat.server.repository.ApplicationRepository;
import com.utic.center.utic.stat.server.service.partition.*;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

@Slf4j
@Getter
@RequiredArgsConstructor
@Service
public class UticStatTableManagerService implements AbstractProcessService {

    private final UticStatTableManagerRepository repo;

    @ProcessingElapsed(type="TABLE", name="Table Partition Manager", starting = false)
    @Override
    public boolean processing() {
        YearMonth next = YearMonth.now().plusMonths(1);
        final String nextMonth = next.format(DateTimeFormatter.ofPattern("yyMM"));       // 예: 2510
        final String commentNextMonth = next.format(DateTimeFormatter.ofPattern("yyyy년 MM월"));

        return createPartitionTable(nextMonth, commentNextMonth);
    }

    public boolean createCurrentPartitionTable() {
        YearMonth current = YearMonth.now();
        final String currMonth = current.format(DateTimeFormatter.ofPattern("yyMM"));    // 예: 2509
        final String commentCurrMonth = current.format(DateTimeFormatter.ofPattern("yyyy년 MM월"));

        return createPartitionTable(currMonth, commentCurrMonth);
    }

    private boolean createPartitionTable(String crtMonth, String commentCrtMonth) {
        // TRAFFIC_CENTER_HIST
        MakeTablePartition trafficCenter = new TrafficCenterHist(this.repo, crtMonth, commentCrtMonth);
        if (!trafficCenter.createPartitionTable()) {
            ApplicationRepository.processState.setError("create TrafficCenterHist table failed.");
            return false;
        }

        // RCV_LINK_TRAFFIC
        MakeTablePartition rcvLinkTraffic = new RcvLinkTraffic(this.repo, crtMonth, commentCrtMonth);
        if (!rcvLinkTraffic.createPartitionTable()) {
            ApplicationRepository.processState.setError("create RcvLinkTraffic table failed.");
            return false;
        }

        // PATN_XXX_HIST
        MakeTablePartition patnHist = new PatternHist(this.repo, crtMonth, commentCrtMonth);
        if (!patnHist.createPartitionTable()) {
            ApplicationRepository.processState.setError("create PatternHist table failed.");
            return false;
        }

        // STAT_LOCAL_LOG
        MakeTablePartition statLocalLog = new StatLocalLog(this.repo, crtMonth, commentCrtMonth);
        if (!statLocalLog.createPartitionTable()) {
            ApplicationRepository.processState.setError("create StatLocalLog table failed.");
            return false;
        }

        // LINK_FILTER_LOG
        MakeTablePartition linkFilterLog = new LinkFilterLog(this.repo, crtMonth, commentCrtMonth);
        if (!linkFilterLog.createPartitionTable()) {
            ApplicationRepository.processState.setError("create LinkFilterLog table failed.");
            return false;
        }

        // STAT_5MIN_CENTER, STAT_5MIN_LOC, STAT_15MIN_CENTER, STAT_15MIN_LOC
        MakeTablePartition statTable = new StatTable(this.repo, crtMonth, commentCrtMonth);
        if (!statTable.createPartitionTable()) {
            ApplicationRepository.processState.setError("create StatTable table failed.");
            return false;
        }

        return true;
    }
}
