// SyncProgressOverlay.m
#import <UIKit/UIKit.h>
#import "SyncProgressOverlay.h"

@implementation SyncProgressOverlay

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (!self) return nil;

    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    UIView *card = [[UIView alloc] init];
    card.backgroundColor = UIColor.whiteColor;
    card.layer.cornerRadius = 12;
    card.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:card];

    [NSLayoutConstraint activateConstraints:@[
        [card.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],
        [card.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],
        [card.widthAnchor constraintEqualToConstant:260]
    ]];

    // Progress label (numeric 1/7)
    self.progressLabel = [[UILabel alloc] init];
    self.progressLabel.textAlignment = NSTextAlignmentCenter;
    self.progressLabel.font = [UIFont boldSystemFontOfSize:16];
    self.progressLabel.translatesAutoresizingMaskIntoConstraints = NO;

    // Message label (descriptive text)
    self.messageLabel = [[UILabel alloc] init];
    self.messageLabel.textAlignment = NSTextAlignmentCenter;
    self.messageLabel.font = [UIFont systemFontOfSize:14];
    self.messageLabel.numberOfLines = 0;
    self.messageLabel.translatesAutoresizingMaskIntoConstraints = NO;

    // Progress bar
    self.progressBar = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    self.progressBar.progress = 0;
    self.progressBar.translatesAutoresizingMaskIntoConstraints = NO;

    // Stack view
    UIStackView *stack = [[UIStackView alloc] initWithArrangedSubviews:@[self.progressLabel, self.messageLabel, self.progressBar]];
    stack.axis = UILayoutConstraintAxisVertical;
    stack.spacing = 12;
    stack.translatesAutoresizingMaskIntoConstraints = NO;
    [card addSubview:stack];

    [NSLayoutConstraint activateConstraints:@[
        [stack.topAnchor constraintEqualToAnchor:card.topAnchor constant:20],
        [stack.leadingAnchor constraintEqualToAnchor:card.leadingAnchor constant:16],
        [stack.trailingAnchor constraintEqualToAnchor:card.trailingAnchor constant:-16],
        [stack.bottomAnchor constraintEqualToAnchor:card.bottomAnchor constant:-20],
    ]];

    return self;
}

// Updated method
- (void)updateStep:(NSInteger)step total:(NSInteger)total message:(NSString *)message {
    self.progressLabel.text = [NSString stringWithFormat:@"%ld / %ld", (long)step, (long)total];
    self.messageLabel.text = message ?: @"";
    self.progressBar.progress = total > 0 ? (float)step / (float)total : 0;
}

@end
