package recycle

import (
    "context"
    "time"

    "gorm.io/gorm"
    "bindbox-game/internal/repository/mysql/dao"
)

type Service interface {
    List(ctx context.Context, typ string, page, size int) (list []map[string]any, total int64, err error)
    Restore(ctx context.Context, typ string, id int64) error
    ForceDelete(ctx context.Context, typ string, id int64) error
}

type service struct{ readDB *dao.Query; writeDB *dao.Query; rdb *gorm.DB; wdb *gorm.DB }

func New(read *dao.Query, write *dao.Query) Service { return &service{readDB: read, writeDB: write} }
func NewRaw(read *dao.Query, write *dao.Query, rdb *gorm.DB, wdb *gorm.DB) Service { return &service{readDB: read, writeDB: write, rdb: rdb, wdb: wdb} }

func (s *service) List(ctx context.Context, typ string, page, size int) (list []map[string]any, total int64, err error) {
    if page <= 0 { page = 1 }
    if size <= 0 { size = 20 }
    db := s.rdb
    if db == nil {
        return nil, 0, nil
    }
    db = db.WithContext(ctx)
    db.InstanceSet("soft_delete_ignore", true)
    switch typ {
    case "activity":
        var rows []struct{ ID int64; Name string; DeletedAt *time.Time }
        if err = db.Table("activities").Where("deleted_at IS NOT NULL").Count(&total).Error; err != nil { return }
        if err = db.Table("activities").Where("deleted_at IS NOT NULL").Limit(size).Offset((page-1)*size).Scan(&rows).Error; err != nil { return }
        list = make([]map[string]any, len(rows))
        for i, r := range rows { list[i] = map[string]any{"id": r.ID, "name": r.Name, "deleted_at": r.DeletedAt} }
    case "issue":
        var rows []struct{ ID int64; IssueNumber string; DeletedAt *time.Time }
        if err = db.Table("activity_issues").Where("deleted_at IS NOT NULL").Count(&total).Error; err != nil { return }
        if err = db.Table("activity_issues").Where("deleted_at IS NOT NULL").Limit(size).Offset((page-1)*size).Scan(&rows).Error; err != nil { return }
        list = make([]map[string]any, len(rows))
        for i, r := range rows { list[i] = map[string]any{"id": r.ID, "name": r.IssueNumber, "deleted_at": r.DeletedAt} }
    case "reward":
        var rows []struct{ ID int64; Name string; DeletedAt *time.Time }
        if err = db.Table("activity_reward_settings").Where("deleted_at IS NOT NULL").Count(&total).Error; err != nil { return }
        if err = db.Table("activity_reward_settings").Where("deleted_at IS NOT NULL").Limit(size).Offset((page-1)*size).Scan(&rows).Error; err != nil { return }
        list = make([]map[string]any, len(rows))
        for i, r := range rows { list[i] = map[string]any{"id": r.ID, "name": r.Name, "deleted_at": r.DeletedAt} }
    case "product":
        var rows []struct{ ID int64; Name string; DeletedAt *time.Time }
        if err = db.Table("products").Where("deleted_at IS NOT NULL").Count(&total).Error; err != nil { return }
        if err = db.Table("products").Where("deleted_at IS NOT NULL").Limit(size).Offset((page-1)*size).Scan(&rows).Error; err != nil { return }
        list = make([]map[string]any, len(rows))
        for i, r := range rows { list[i] = map[string]any{"id": r.ID, "name": r.Name, "deleted_at": r.DeletedAt} }
    case "category":
        var rows []struct{ ID int64; Name string; DeletedAt *time.Time }
        if err = db.Table("product_categories").Where("deleted_at IS NOT NULL").Count(&total).Error; err != nil { return }
        if err = db.Table("product_categories").Where("deleted_at IS NOT NULL").Limit(size).Offset((page-1)*size).Scan(&rows).Error; err != nil { return }
        list = make([]map[string]any, len(rows))
        for i, r := range rows { list[i] = map[string]any{"id": r.ID, "name": r.Name, "deleted_at": r.DeletedAt} }
    case "banner":
        var rows []struct{ ID int64; Title string; DeletedAt *time.Time }
        if err = db.Table("banner").Where("deleted_at IS NOT NULL").Count(&total).Error; err != nil { return }
        if err = db.Table("banner").Where("deleted_at IS NOT NULL").Limit(size).Offset((page-1)*size).Scan(&rows).Error; err != nil { return }
        list = make([]map[string]any, len(rows))
        for i, r := range rows { list[i] = map[string]any{"id": r.ID, "title": r.Title, "deleted_at": r.DeletedAt} }
    case "item_card":
        var rows []struct{ ID int64; Name string; DeletedAt *time.Time }
        if err = db.Table("system_item_cards").Where("deleted_at IS NOT NULL").Count(&total).Error; err != nil { return }
        if err = db.Table("system_item_cards").Where("deleted_at IS NOT NULL").Limit(size).Offset((page-1)*size).Scan(&rows).Error; err != nil { return }
        list = make([]map[string]any, len(rows))
        for i, r := range rows { list[i] = map[string]any{"id": r.ID, "name": r.Name, "deleted_at": r.DeletedAt} }
    case "coupon":
        var rows []struct{ ID int64; Name string; DeletedAt *time.Time }
        if err = db.Table("system_coupons").Where("deleted_at IS NOT NULL").Count(&total).Error; err != nil { return }
        if err = db.Table("system_coupons").Where("deleted_at IS NOT NULL").Limit(size).Offset((page-1)*size).Scan(&rows).Error; err != nil { return }
        list = make([]map[string]any, len(rows))
        for i, r := range rows { list[i] = map[string]any{"id": r.ID, "name": r.Name, "deleted_at": r.DeletedAt} }
    case "menu":
        var rows []struct{ ID int64; Name string; DeletedAt *time.Time }
        if err = db.Table("menus").Where("deleted_at IS NOT NULL").Count(&total).Error; err != nil { return }
        if err = db.Table("menus").Where("deleted_at IS NOT NULL").Limit(size).Offset((page-1)*size).Scan(&rows).Error; err != nil { return }
        list = make([]map[string]any, len(rows))
        for i, r := range rows { list[i] = map[string]any{"id": r.ID, "name": r.Name, "deleted_at": r.DeletedAt} }
    case "menu_action":
        var rows []struct{ ID int64; ActionName string; DeletedAt *time.Time }
        if err = db.Table("menu_actions").Where("deleted_at IS NOT NULL").Count(&total).Error; err != nil { return }
        if err = db.Table("menu_actions").Where("deleted_at IS NOT NULL").Limit(size).Offset((page-1)*size).Scan(&rows).Error; err != nil { return }
        list = make([]map[string]any, len(rows))
        for i, r := range rows { list[i] = map[string]any{"id": r.ID, "action_name": r.ActionName, "deleted_at": r.DeletedAt} }
    case "role":
        var rows []struct{ ID int64; RoleName string; DeletedAt *time.Time }
        if err = db.Table("roles").Where("deleted_at IS NOT NULL").Count(&total).Error; err != nil { return }
        if err = db.Table("roles").Where("deleted_at IS NOT NULL").Limit(size).Offset((page-1)*size).Scan(&rows).Error; err != nil { return }
        list = make([]map[string]any, len(rows))
        for i, r := range rows { list[i] = map[string]any{"id": r.ID, "role_name": r.RoleName, "deleted_at": r.DeletedAt} }
    case "role_user":
        var rows []struct{ ID int64; RoleID int64; AdminID int32; DeletedAt *time.Time }
        if err = db.Table("role_users").Where("deleted_at IS NOT NULL").Count(&total).Error; err != nil { return }
        if err = db.Table("role_users").Where("deleted_at IS NOT NULL").Limit(size).Offset((page-1)*size).Scan(&rows).Error; err != nil { return }
        list = make([]map[string]any, len(rows))
        for i, r := range rows { list[i] = map[string]any{"id": r.ID, "role_id": r.RoleID, "admin_id": r.AdminID, "deleted_at": r.DeletedAt} }
    default:
        total, list, err = 0, nil, nil
    }
    return
}

func (s *service) Restore(ctx context.Context, typ string, id int64) error {
    switch typ {
    case "activity":
        _, err := s.writeDB.Activities.WithContext(ctx).Where(s.writeDB.Activities.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    case "issue":
        _, err := s.writeDB.ActivityIssues.WithContext(ctx).Where(s.writeDB.ActivityIssues.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    case "reward":
        _, err := s.writeDB.ActivityRewardSettings.WithContext(ctx).Where(s.writeDB.ActivityRewardSettings.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    case "product":
        _, err := s.writeDB.Products.WithContext(ctx).Where(s.writeDB.Products.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    case "category":
        _, err := s.writeDB.ProductCategories.WithContext(ctx).Where(s.writeDB.ProductCategories.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    case "banner":
        _, err := s.writeDB.Banner.WithContext(ctx).Where(s.writeDB.Banner.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    case "title":
        _, err := s.writeDB.SystemTitles.WithContext(ctx).Where(s.writeDB.SystemTitles.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    case "title_effect":
        _, err := s.writeDB.SystemTitleEffects.WithContext(ctx).Where(s.writeDB.SystemTitleEffects.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    case "item_card":
        _, err := s.writeDB.SystemItemCards.WithContext(ctx).Where(s.writeDB.SystemItemCards.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    case "coupon":
        _, err := s.writeDB.SystemCoupons.WithContext(ctx).Where(s.writeDB.SystemCoupons.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    case "menu":
        _, err := s.writeDB.Menus.WithContext(ctx).Where(s.writeDB.Menus.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    case "menu_action":
        _, err := s.writeDB.MenuActions.WithContext(ctx).Where(s.writeDB.MenuActions.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    case "role":
        _, err := s.writeDB.Roles.WithContext(ctx).Where(s.writeDB.Roles.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    case "role_user":
        _, err := s.writeDB.RoleUsers.WithContext(ctx).Where(s.writeDB.RoleUsers.ID.Eq(id)).Updates(map[string]any{"deleted_at": nil, "deleted_by": nil}); return err
    default:
        return nil
    }
}

func (s *service) ForceDelete(ctx context.Context, typ string, id int64) error {
    switch typ {
    case "activity":
        _, err := s.writeDB.Activities.WithContext(ctx).Where(s.writeDB.Activities.ID.Eq(id)).Delete(); return err
    case "issue":
        _, err := s.writeDB.ActivityIssues.WithContext(ctx).Where(s.writeDB.ActivityIssues.ID.Eq(id)).Delete(); return err
    case "reward":
        _, err := s.writeDB.ActivityRewardSettings.WithContext(ctx).Where(s.writeDB.ActivityRewardSettings.ID.Eq(id)).Delete(); return err
    case "product":
        _, err := s.writeDB.Products.WithContext(ctx).Where(s.writeDB.Products.ID.Eq(id)).Delete(); return err
    case "category":
        _, err := s.writeDB.ProductCategories.WithContext(ctx).Where(s.writeDB.ProductCategories.ID.Eq(id)).Delete(); return err
    case "banner":
        _, err := s.writeDB.Banner.WithContext(ctx).Where(s.writeDB.Banner.ID.Eq(id)).Delete(); return err
    case "title":
        _, err := s.writeDB.SystemTitles.WithContext(ctx).Where(s.writeDB.SystemTitles.ID.Eq(id)).Delete(); return err
    case "title_effect":
        _, err := s.writeDB.SystemTitleEffects.WithContext(ctx).Where(s.writeDB.SystemTitleEffects.ID.Eq(id)).Delete(); return err
    case "item_card":
        _, err := s.writeDB.SystemItemCards.WithContext(ctx).Where(s.writeDB.SystemItemCards.ID.Eq(id)).Delete(); return err
    case "coupon":
        _, err := s.writeDB.SystemCoupons.WithContext(ctx).Where(s.writeDB.SystemCoupons.ID.Eq(id)).Delete(); return err
    case "menu":
        _, err := s.writeDB.Menus.WithContext(ctx).Where(s.writeDB.Menus.ID.Eq(id)).Delete(); return err
    case "menu_action":
        _, err := s.writeDB.MenuActions.WithContext(ctx).Where(s.writeDB.MenuActions.ID.Eq(id)).Delete(); return err
    case "role":
        _, err := s.writeDB.Roles.WithContext(ctx).Where(s.writeDB.Roles.ID.Eq(id)).Delete(); return err
    case "role_user":
        _, err := s.writeDB.RoleUsers.WithContext(ctx).Where(s.writeDB.RoleUsers.ID.Eq(id)).Delete(); return err
    default:
        return nil
    }
}
