package seeds

import (
	"gorm.io/gorm"
	"netidhub-saas-be/app/database/entity"
)

type ApprovalWorkflowsSeeder struct{}

// Sample 3-level approval workflow
var approvalWorkflows = []entity.ApprovalWorkflows{
	{
		ID:          1,
		Name:        "3-Level Approval Workflow",
		Description: &[]string{"Standard 3-level approval workflow for articles: Editor -> Senior Editor -> Chief Editor"}[0],
		IsActive:    &[]bool{true}[0],
	},
	{
		ID:          2,
		Name:        "2-Level Approval Workflow",
		Description: &[]string{"Simple 2-level approval workflow: Editor -> Chief Editor"}[0],
		IsActive:    &[]bool{true}[0],
	},
}

// Sample approval workflow steps for 3-level workflow
var approvalWorkflowSteps = []entity.ApprovalWorkflowSteps{
	// 3-Level Workflow Steps
	{
		ID:                    1,
		WorkflowId:            1,
		StepOrder:             1,
		StepName:              "Editor Review",
		RequiredUserLevelId:   3, // Assuming Editor user level ID is 3
		CanSkip:               &[]bool{false}[0],
		AutoApproveAfterHours: &[]int{24}[0],
		IsActive:              &[]bool{true}[0],
	},
	{
		ID:                    2,
		WorkflowId:            1,
		StepOrder:             2,
		StepName:              "Senior Editor Review",
		RequiredUserLevelId:   4, // Assuming Senior Editor user level ID is 4
		CanSkip:               &[]bool{false}[0],
		AutoApproveAfterHours: &[]int{48}[0],
		IsActive:              &[]bool{true}[0],
	},
	{
		ID:                    3,
		WorkflowId:            1,
		StepOrder:             3,
		StepName:              "Chief Editor Final Approval",
		RequiredUserLevelId:   5, // Assuming Chief Editor user level ID is 5
		CanSkip:               &[]bool{false}[0],
		AutoApproveAfterHours: &[]int{72}[0],
		IsActive:              &[]bool{true}[0],
	},
	// 2-Level Workflow Steps
	{
		ID:                    4,
		WorkflowId:            2,
		StepOrder:             1,
		StepName:              "Editor Review",
		RequiredUserLevelId:   3, // Editor user level
		CanSkip:               &[]bool{false}[0],
		AutoApproveAfterHours: &[]int{24}[0],
		IsActive:              &[]bool{true}[0],
	},
	{
		ID:                    5,
		WorkflowId:            2,
		StepOrder:             2,
		StepName:              "Chief Editor Approval",
		RequiredUserLevelId:   5, // Chief Editor user level
		CanSkip:               &[]bool{false}[0],
		AutoApproveAfterHours: &[]int{48}[0],
		IsActive:              &[]bool{true}[0],
	},
}

func (ApprovalWorkflowsSeeder) Seed(conn *gorm.DB) error {
	// Seed approval workflows
	for _, workflow := range approvalWorkflows {
		if err := conn.Create(&workflow).Error; err != nil {
			return err
		}
	}

	// Seed approval workflow steps
	for _, step := range approvalWorkflowSteps {
		if err := conn.Create(&step).Error; err != nil {
			return err
		}
	}

	return nil
}

func (ApprovalWorkflowsSeeder) Count(conn *gorm.DB) (int, error) {
	var workflowCount int64
	if err := conn.Model(&entity.ApprovalWorkflows{}).Count(&workflowCount).Error; err != nil {
		return 0, err
	}

	var stepCount int64
	if err := conn.Model(&entity.ApprovalWorkflowSteps{}).Count(&stepCount).Error; err != nil {
		return 0, err
	}

	return int(workflowCount + stepCount), nil
}
