  //  ThirdDetailsViewController.mm

#import "ThirdDetailsViewController.h"
#import "APICLient.h"

@interface ThirdPartyDetailsViewController ()

// Header
@property (nonatomic, strong) UIView *headerBar;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *backButton;

// Main Content
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UIImageView *qrImageView;
@property (nonatomic, strong) UILabel *linkTitleLabel;
@property (nonatomic, strong) UILabel *createdOnLabel;
@property (nonatomic, strong) UILabel *linkStatusLabel;
@property (nonatomic, strong) UIButton *buttonCopy;
@property (nonatomic, strong) UIButton *shareButton;
@property (nonatomic, strong) UISwitch *accessSwitch;
@property (nonatomic, strong) UIButton *changeNameButton;
@property (nonatomic, strong) UIView *topContentView;

@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UIButton *deleteFooterButton;

@property (nonatomic, strong) UIView *accessContainer;
@property (nonatomic, strong) UILabel *accessLabel;
@property (nonatomic, strong) UILabel *changeNameLabel;
@property (nonatomic, strong) UIImageView *arrowImageView;

// Loading overlay
@property (nonatomic, strong) UIView *loadingOverlay;

@property (nonatomic, strong) UIView *dimmedOverlay;
@property (nonatomic, strong) UIView *bottomSheet;
@property (nonatomic, strong) NSLayoutConstraint *bottomSheetBottomConstraint;

// For change name
@property (nonatomic, strong) UITextField *linkNameField;
@property (nonatomic, strong) UIButton *primaryButton;
@property (nonatomic, strong) UIView *addLinkSheet;
@property (nonatomic, strong) UIButton *createButton;

// Success modal
@property (nonatomic, strong) UIView *successOverlay;
@property (nonatomic, strong) UIView *successModal;

@property (nonatomic, strong) UIView *confirmOverlay;
@property (nonatomic, strong) UIView *confirmModal;

@end

@implementation ThirdPartyDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.systemBackgroundColor;
  
    NSLog(@"third party unit: %@", self.item);
    
    [self setupHeader];
    [self setupFooter];
    [self setupScrollView];  // Setup scrollView FIRST
    [self setupContent];     // Then setup content INSIDE scrollView
    [self setupLoadingOverlay];
    [self updateUI];
  
    // Debug layout
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"TopContentView frame: %@", NSStringFromCGRect(self.topContentView.frame));
        NSLog(@"AccessContainer frame: %@", NSStringFromCGRect(self.accessContainer.frame));
        NSLog(@"ContentView frame: %@", NSStringFromCGRect(self.contentView.frame));
    });
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    NSLog(@"After layout:");
    NSLog(@"TopContentView frame: %@", NSStringFromCGRect(self.topContentView.frame));
    NSLog(@"AccessContainer frame: %@", NSStringFromCGRect(self.accessContainer.frame));
    NSLog(@"ContentView frame: %@", NSStringFromCGRect(self.contentView.frame));
    NSLog(@"ScrollView contentSize: %@", NSStringFromCGSize(self.scrollView.contentSize));
}

#pragma mark - Header
- (void)setupHeader {
    UILayoutGuide *safe = self.view.safeAreaLayoutGuide;
    
    self.headerBar = [[UIView alloc] init];
    self.headerBar.backgroundColor = [UIColor colorWithRed:0.0 green:0.43 blue:0.55 alpha:1.0]; // #006D8D
    [self.view addSubview:self.headerBar];
    self.headerBar.translatesAutoresizingMaskIntoConstraints = NO;
    
    [NSLayoutConstraint activateConstraints:@[
        [self.headerBar.topAnchor constraintEqualToAnchor:safe.topAnchor],
        [self.headerBar.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [self.headerBar.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [self.headerBar.heightAnchor constraintEqualToConstant:44],
    ]];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"Manage 3rd Party Link";
    self.titleLabel.textColor = UIColor.whiteColor;
    self.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.backButton setImage:[UIImage systemImageNamed:@"chevron.backward"] forState:UIControlStateNormal];
    self.backButton.tintColor = UIColor.whiteColor;
    [self.backButton addTarget:self
                        action:@selector(onBack)
              forControlEvents:UIControlEventTouchUpInside];
    
    for (UIView *sub in @[self.titleLabel, self.backButton]) {
        sub.translatesAutoresizingMaskIntoConstraints = NO;
        [self.headerBar addSubview:sub];
    }
    
    [NSLayoutConstraint activateConstraints:@[
        [self.backButton.leadingAnchor constraintEqualToAnchor:self.headerBar.leadingAnchor constant:16],
        [self.backButton.centerYAnchor constraintEqualToAnchor:self.headerBar.centerYAnchor],
        [self.backButton.heightAnchor constraintEqualToConstant:24],
        [self.backButton.widthAnchor constraintEqualToConstant:24],
        
        [self.titleLabel.centerXAnchor constraintEqualToAnchor:self.headerBar.centerXAnchor],
        [self.titleLabel.centerYAnchor constraintEqualToAnchor:self.headerBar.centerYAnchor],
    ]];
}

#pragma mark - ScrollView + Content View
- (void)setupScrollView {
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.scrollView];
    
    self.contentView = [[UIView alloc] init];
    self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.scrollView addSubview:self.contentView];
    
    // ScrollView constraints
    [NSLayoutConstraint activateConstraints:@[
        [self.scrollView.topAnchor constraintEqualToAnchor:self.headerBar.bottomAnchor],
        [self.scrollView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [self.scrollView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [self.scrollView.bottomAnchor constraintEqualToAnchor:self.footerView.topAnchor],
    ]];
    
    // ContentView constraints - FIXED: Connect all 4 sides to scrollView
    [NSLayoutConstraint activateConstraints:@[
        [self.contentView.topAnchor constraintEqualToAnchor:self.scrollView.topAnchor],
        [self.contentView.leadingAnchor constraintEqualToAnchor:self.scrollView.leadingAnchor],
        [self.contentView.trailingAnchor constraintEqualToAnchor:self.scrollView.trailingAnchor],
        [self.contentView.bottomAnchor constraintEqualToAnchor:self.scrollView.bottomAnchor],
        [self.contentView.widthAnchor constraintEqualToAnchor:self.scrollView.widthAnchor],
        // Height will be determined by content
    ]];
}

#pragma mark - Content (FINAL WORKING VERSION)
- (void)setupContent {
    // Clear any existing views
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    // ========== CREATE ALL VIEWS ==========
    
    // QR Code
    self.qrImageView = [[UIImageView alloc] init];
    self.qrImageView.backgroundColor = UIColor.systemGray5Color;
    self.qrImageView.layer.cornerRadius = 12;
    self.qrImageView.clipsToBounds = YES;
    self.qrImageView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.qrImageView];
    
    // Link title
    self.linkTitleLabel = [[UILabel alloc] init];
    self.linkTitleLabel.text = @"Link Title";
    self.linkTitleLabel.font = [UIFont boldSystemFontOfSize:18];
    self.linkTitleLabel.textAlignment = NSTextAlignmentCenter;
    self.linkTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.linkTitleLabel];
    
    self.createdOnLabel = [[UILabel alloc] init];
    self.createdOnLabel.text = @"Created On ";
    self.createdOnLabel.font = [UIFont systemFontOfSize:14];
    self.createdOnLabel.textAlignment = NSTextAlignmentCenter;
    self.createdOnLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.createdOnLabel];
    
    // Status pill
    self.linkStatusLabel = [[UILabel alloc] init];
    self.linkStatusLabel.text = @"Active";
    self.linkStatusLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    self.linkStatusLabel.textColor = UIColor.whiteColor;
    self.linkStatusLabel.textAlignment = NSTextAlignmentCenter;
    self.linkStatusLabel.backgroundColor = [UIColor systemGreenColor];
    self.linkStatusLabel.layer.cornerRadius = 12;
    self.linkStatusLabel.clipsToBounds = YES;
    self.linkStatusLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.linkStatusLabel];
    
    // Buttons - CRITICAL: Create them with the helper method
    self.buttonCopy = [self actionButtonWithImage:@"link" title:@"Copy Link"];
    [self.buttonCopy addTarget:self action:@selector(handleCopyLink) forControlEvents:UIControlEventTouchUpInside];
    self.buttonCopy.translatesAutoresizingMaskIntoConstraints = NO; // Ensure this is set
    [self.contentView addSubview:self.buttonCopy];
    
    UIView *divider = [[UIView alloc] init];
    divider.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1.0];
    divider.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:divider];
    
    self.shareButton = [self actionButtonWithImage:@"arrowshape.turn.up.right" title:@"Share"];
    [self.shareButton addTarget:self action:@selector(handleShareLink) forControlEvents:UIControlEventTouchUpInside];
    self.shareButton.translatesAutoresizingMaskIntoConstraints = NO; // Ensure this is set
    [self.contentView addSubview:self.shareButton];
    
    // Access Container
    self.accessContainer = [[UIView alloc] init];
    self.accessContainer.backgroundColor = UIColor.whiteColor;
    self.accessContainer.layer.cornerRadius = 24;
    self.accessContainer.layer.borderWidth = 0.5;
    self.accessContainer.layer.borderColor = [UIColor colorWithWhite:0.75 alpha:1.0].CGColor;
    self.accessContainer.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.accessContainer];
    
    // Access Container internal views
    UILabel *accessLabel = [[UILabel alloc] init];
    accessLabel.text = @"Link Access";
    accessLabel.font = [UIFont systemFontOfSize:16];
    accessLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.accessContainer addSubview:accessLabel];
    
    self.accessSwitch = [[UISwitch alloc] init];
    self.accessSwitch.on = YES;
    self.accessSwitch.translatesAutoresizingMaskIntoConstraints = NO;
    [self.accessSwitch addTarget:self action:@selector(handleToggleAccess:) forControlEvents:UIControlEventValueChanged];
    [self.accessContainer addSubview:self.accessSwitch];
    
    UIView *innerDivider = [[UIView alloc] init];
    innerDivider.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
    innerDivider.translatesAutoresizingMaskIntoConstraints = NO;
    [self.accessContainer addSubview:innerDivider];
    
    self.changeNameButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.changeNameButton setTitle:@"Change Link Name" forState:UIControlStateNormal];
    self.changeNameButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.changeNameButton.titleLabel.font = [UIFont systemFontOfSize:16];
    self.changeNameButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.changeNameButton addTarget:self action:@selector(handleChangeName) forControlEvents:UIControlEventTouchUpInside];
    [self.accessContainer addSubview:self.changeNameButton];
    
    UIImageView *arrowImage = [[UIImageView alloc] initWithImage:[UIImage systemImageNamed:@"chevron.right"]];
    arrowImage.tintColor = UIColor.systemGrayColor;
    arrowImage.translatesAutoresizingMaskIntoConstraints = NO;
    [self.accessContainer addSubview:arrowImage];
    
    // ========== SETUP ALL CONSTRAINTS ==========
    
    // 1. QR Image
    [NSLayoutConstraint activateConstraints:@[
        [self.qrImageView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:24],
        [self.qrImageView.centerXAnchor constraintEqualToAnchor:self.contentView.centerXAnchor],
        [self.qrImageView.widthAnchor constraintEqualToConstant:148],
        [self.qrImageView.heightAnchor constraintEqualToConstant:148],
    ]];
    
    // 2. Title Label
    [NSLayoutConstraint activateConstraints:@[
        [self.linkTitleLabel.topAnchor constraintEqualToAnchor:self.qrImageView.bottomAnchor constant:28],
        [self.linkTitleLabel.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:20],
        [self.linkTitleLabel.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-20],
    ]];
    
    // 3. Created On Label
    [NSLayoutConstraint activateConstraints:@[
        [self.createdOnLabel.topAnchor constraintEqualToAnchor:self.linkTitleLabel.bottomAnchor constant:8],
        [self.createdOnLabel.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:20],
        [self.createdOnLabel.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-20],
    ]];
    
    // 4. Status Label
    [NSLayoutConstraint activateConstraints:@[
        [self.linkStatusLabel.topAnchor constraintEqualToAnchor:self.createdOnLabel.bottomAnchor constant:28],
        [self.linkStatusLabel.centerXAnchor constraintEqualToAnchor:self.contentView.centerXAnchor],
        [self.linkStatusLabel.widthAnchor constraintEqualToConstant:80],
        [self.linkStatusLabel.heightAnchor constraintEqualToConstant:24],
    ]];
    
    // 5. Buttons Row - FIXED
    [NSLayoutConstraint activateConstraints:@[
        // Copy Button
        [self.buttonCopy.topAnchor constraintEqualToAnchor:self.linkStatusLabel.bottomAnchor constant:32],
        [self.buttonCopy.trailingAnchor constraintEqualToAnchor:self.contentView.centerXAnchor constant:-12],
        [self.buttonCopy.widthAnchor constraintEqualToConstant:105],
        [self.buttonCopy.heightAnchor constraintEqualToConstant:48],
        
        // Divider
        [divider.centerXAnchor constraintEqualToAnchor:self.contentView.centerXAnchor],
        [divider.centerYAnchor constraintEqualToAnchor:self.buttonCopy.centerYAnchor],
        [divider.widthAnchor constraintEqualToConstant:1],
        [divider.heightAnchor constraintEqualToConstant:44],
        
        // Share Button
        [self.shareButton.topAnchor constraintEqualToAnchor:self.buttonCopy.topAnchor],
        [self.shareButton.leadingAnchor constraintEqualToAnchor:self.contentView.centerXAnchor constant:12],
        [self.shareButton.widthAnchor constraintEqualToConstant:105],
        [self.shareButton.heightAnchor constraintEqualToConstant:48],
    ]];
    
    // 6. Access Container
    [NSLayoutConstraint activateConstraints:@[
        [self.accessContainer.topAnchor constraintEqualToAnchor:self.buttonCopy.bottomAnchor constant:36],
        [self.accessContainer.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:20],
        [self.accessContainer.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-20],
        [self.accessContainer.heightAnchor constraintEqualToConstant:125],
    ]];
    
    // 7. Access Container Internal
    [NSLayoutConstraint activateConstraints:@[
        [accessLabel.topAnchor constraintEqualToAnchor:self.accessContainer.topAnchor constant:16],
        [accessLabel.leadingAnchor constraintEqualToAnchor:self.accessContainer.leadingAnchor constant:20],
        
        [self.accessSwitch.centerYAnchor constraintEqualToAnchor:accessLabel.centerYAnchor],
        [self.accessSwitch.trailingAnchor constraintEqualToAnchor:self.accessContainer.trailingAnchor constant:-20],
        
        [innerDivider.leadingAnchor constraintEqualToAnchor:self.accessContainer.leadingAnchor constant:20],
        [innerDivider.trailingAnchor constraintEqualToAnchor:self.accessContainer.trailingAnchor constant:-20],
        [innerDivider.centerYAnchor constraintEqualToAnchor:self.accessContainer.centerYAnchor],
        [innerDivider.heightAnchor constraintEqualToConstant:1],
        
        [self.changeNameButton.topAnchor constraintEqualToAnchor:innerDivider.bottomAnchor constant:12],
        [self.changeNameButton.leadingAnchor constraintEqualToAnchor:self.accessContainer.leadingAnchor constant:20],
        [self.changeNameButton.trailingAnchor constraintEqualToAnchor:arrowImage.leadingAnchor constant:-8],
        [self.changeNameButton.bottomAnchor constraintEqualToAnchor:self.accessContainer.bottomAnchor constant:-12],
        
        [arrowImage.centerYAnchor constraintEqualToAnchor:self.changeNameButton.centerYAnchor],
        [arrowImage.trailingAnchor constraintEqualToAnchor:self.accessContainer.trailingAnchor constant:-20],
        [arrowImage.widthAnchor constraintEqualToConstant:12],
        [arrowImage.heightAnchor constraintEqualToConstant:24],
    ]];
    
    // 8. ContentView Bottom (defines scrollable height)
    [NSLayoutConstraint activateConstraints:@[
        [self.contentView.bottomAnchor constraintEqualToAnchor:self.accessContainer.bottomAnchor constant:100],
    ]];
}

- (UIButton *)actionButtonWithImage:(NSString *)imageName title:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.tintColor = [UIColor colorWithRed:0.05 green:0.45 blue:0.57 alpha:1.0]; // #0E7392
    button.translatesAutoresizingMaskIntoConstraints = NO; // CRITICAL: Add this!

    UIImage *image = [UIImage systemImageNamed:imageName];
    if (!image) {
        image = [UIImage systemImageNamed:@"link"];
    }
    
    [button setImage:image forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];

    button.titleLabel.font = [UIFont systemFontOfSize:14];
    
    // Simple layout - image above text
    button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    // Set image and title edge insets for vertical layout
    CGFloat imageHeight = 24; // Approximate image height
    CGFloat spacing = 4; // Space between image and text
    
    button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, title.length > 0 ? spacing + 10 : 0, 0);
    button.titleEdgeInsets = UIEdgeInsetsMake(imageHeight + spacing, -image.size.width, 0, 0);
    
    return button;
}

#pragma mark - Footer (Delete Button)
- (void)setupFooter {
    UILayoutGuide *safe = self.view.safeAreaLayoutGuide;

    self.footerView = [[UIView alloc] init];
    self.footerView.translatesAutoresizingMaskIntoConstraints = NO;
    self.footerView.backgroundColor = UIColor.clearColor;
    [self.view addSubview:self.footerView];

    self.deleteFooterButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.deleteFooterButton setTitle:@"Delete Link" forState:UIControlStateNormal];
    [self.deleteFooterButton setTitleColor:[UIColor colorWithRed:1.0 green:0.39 blue:0.39 alpha:1.0] forState:UIControlStateNormal];
    self.deleteFooterButton.layer.cornerRadius = 20;
    self.deleteFooterButton.layer.borderWidth = 0.5;
    self.deleteFooterButton.layer.borderColor = [UIColor colorWithRed:1.0 green:0.39 blue:0.39 alpha:1.0].CGColor;
    self.deleteFooterButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.deleteFooterButton addTarget:self action:@selector(presentDeleteConfirmModal) forControlEvents:UIControlEventTouchUpInside];

    [self.footerView addSubview:self.deleteFooterButton];

    [NSLayoutConstraint activateConstraints:@[
        // Footer container
        [self.footerView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [self.footerView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [self.footerView.bottomAnchor constraintEqualToAnchor:safe.bottomAnchor],
        [self.footerView.heightAnchor constraintEqualToConstant:88],

        // Button inside footer
        [self.deleteFooterButton.topAnchor constraintEqualToAnchor:self.footerView.topAnchor constant:16],
        [self.deleteFooterButton.heightAnchor constraintEqualToConstant:48],
        [self.deleteFooterButton.centerXAnchor constraintEqualToAnchor:self.footerView.centerXAnchor],
        [self.deleteFooterButton.widthAnchor constraintEqualToConstant:245],
    ]];
}

#pragma mark - Loading Overlay
- (void)setupLoadingOverlay {
    self.loadingOverlay = [[UIView alloc] init];
    self.loadingOverlay.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    self.loadingOverlay.hidden = YES;
    self.loadingOverlay.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.loadingOverlay];
    
    [NSLayoutConstraint activateConstraints:@[
        [self.loadingOverlay.topAnchor constraintEqualToAnchor:self.view.topAnchor],
        [self.loadingOverlay.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [self.loadingOverlay.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [self.loadingOverlay.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
    ]];
}

#pragma mark - Actions
- (void)onBack {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void) updateUI{
  if (!self.item) return;
  
  self.linkTitleLabel.text = self.item[@"title"] ?: @"No Title";
  
  NSString *createdOn = self.item[@"created_at"]; // e.g. "15-01-2026"

  NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
  [inputFormatter setDateFormat:@"dd-MM-yyyy"]; // matches the input string

  NSDate *date = [inputFormatter dateFromString:createdOn];
  if (date) {
      NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
      [outputFormatter setDateFormat:@"dd MMM yyyy"]; // desired output, e.g. 15 Jan 2026
      NSString *formattedDate = [outputFormatter stringFromDate:date];
      self.createdOnLabel.text = [NSString stringWithFormat:@"Created on %@", formattedDate];
  } else {
      self.createdOnLabel.text = [NSString stringWithFormat:@"Created on %@", createdOn]; // fallback
  }
  
  BOOL isActive = [self.item[@"status"] integerValue] == 1;
  self.accessSwitch.on = isActive;
  
  self.linkStatusLabel.text = isActive ? @"ACTIVE" : @"INACTIVE";
  self.linkStatusLabel.backgroundColor = isActive ? UIColor.systemGreenColor : UIColor.systemGrayColor;
  
  // QR code generation (simple placeholder)
  NSString *urlCode = self.item[@"url_code"];
  NSString *path = self.item[@"path"]; // "https://sptest.commudesk.com"
  NSString *fullURL = [NSString stringWithFormat:@"%@/%@", path, urlCode];

  // Generate QR code
  self.qrImageView.image = [self generateQRCodeFromString:fullURL];
  
  [self.contentView setNeedsLayout];
  [self.contentView layoutIfNeeded];
}

- (UIImage *)generateQRCodeFromString:(NSString *)string {
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setValue:data forKey:@"inputMessage"];
    [filter setValue:@"M" forKey:@"inputCorrectionLevel"];

    CIImage *ciImage = filter.outputImage;
    CGRect rect = CGRectIntegral(ciImage.extent);
    CGFloat scale = 154 / CGRectGetWidth(rect);

    size_t width = CGRectGetWidth(rect) * scale;
    size_t height = CGRectGetHeight(rect) * scale;

    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate(nil, width, height, 8, 0, cs, kCGImageAlphaNone);
    CIContext *ciContext = [CIContext context];
    CGImageRef imageRef = [ciContext createCGImage:ciImage fromRect:rect];

    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextScaleCTM(context, scale, scale);
    CGContextDrawImage(context, rect, imageRef);

    CGImageRef scaledImage = CGBitmapContextCreateImage(context);
    UIImage *image = [UIImage imageWithCGImage:scaledImage];

    CGContextRelease(context);
    CGColorSpaceRelease(cs);
    CGImageRelease(imageRef);
    CGImageRelease(scaledImage);

    return image;
}

- (void)handleCopyLink {
    NSString *path = self.item[@"path"] ?: @"";
    NSString *urlCode = self.item[@"url_code"] ?: @"";
    if (!path || !urlCode) return;

    NSString *fullURL = [NSString stringWithFormat:@"%@/%@", path, urlCode];
    UIPasteboard.generalPasteboard.string = fullURL;

    [self showToast:@"Link Copied"];
}

- (void)handleShareLink {
    self.dimmedOverlay = [self buildDimmedOverlay];
    self.bottomSheet = [self buildShareSheet];

    [self.view addSubview:self.dimmedOverlay];
    [self.view addSubview:self.bottomSheet];

    NSLayoutConstraint *bottom =
        [self.bottomSheet.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:300];
    bottom.active = YES;

    [NSLayoutConstraint activateConstraints:@[
        [self.bottomSheet.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [self.bottomSheet.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [self.bottomSheet.heightAnchor constraintEqualToConstant:320],
    ]];

    [self.view layoutIfNeeded];

    [UIView animateWithDuration:0.3 animations:^{
        self.dimmedOverlay.alpha = 1;
        bottom.constant = 0;
        [self.view layoutIfNeeded];
    }];
}

- (void)handleChangeName {
    NSLog(@"Show change name modal");
    [self showAddLinkSheet];
}

- (void)showToast:(NSString *)message {
    if (!message || message.length == 0) return;

    CGFloat toastWidth = 250;
    CGFloat toastHeight = 50;

    UILabel *toastLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    toastLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    toastLabel.textColor = UIColor.whiteColor;
    toastLabel.textAlignment = NSTextAlignmentCenter;
    toastLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
    toastLabel.text = message;
    toastLabel.alpha = 0.0;
    toastLabel.numberOfLines = 0;
    toastLabel.layer.cornerRadius = 12;
    toastLabel.clipsToBounds = YES;
    toastLabel.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:toastLabel];

    // Center it horizontally and place it near the bottom
    [NSLayoutConstraint activateConstraints:@[
        [toastLabel.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
        [toastLabel.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor constant:-80],
        [toastLabel.widthAnchor constraintEqualToConstant:toastWidth],
        [toastLabel.heightAnchor constraintEqualToConstant:toastHeight],
    ]];

    // Animate toast in
    [UIView animateWithDuration:0.3 animations:^{
        toastLabel.alpha = 1.0;
    } completion:^(BOOL finished) {
        // After 1.5 seconds, fade it out
        [UIView animateWithDuration:0.3 delay:1.5 options:0 animations:^{
            toastLabel.alpha = 0.0;
        } completion:^(BOOL finished) {
            [toastLabel removeFromSuperview];
        }];
    }];
}

- (void)showAlert:(NSString *)title message:(NSString *)message {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - Overlays
- (UIView *)buildDimmedOverlay {
    UIView *overlay = [[UIView alloc] initWithFrame:self.view.bounds];
    overlay.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    overlay.alpha = 0;

    UITapGestureRecognizer *tap =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                action:@selector(dismissBottomSheet)];
    [overlay addGestureRecognizer:tap];

    return overlay;
}

- (UIView *)buildAddLinkSheet {
    UIView *sheet = [[UIView alloc] init];
    sheet.backgroundColor = UIColor.whiteColor;
    sheet.layer.cornerRadius = 24;
    sheet.layer.maskedCorners =
        kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
    sheet.translatesAutoresizingMaskIntoConstraints = NO;

    UILabel *title = [[UILabel alloc] init];
    title.text = @"Change 3rd Party Link";
    title.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    title.textAlignment = NSTextAlignmentCenter;
    title.translatesAutoresizingMaskIntoConstraints = NO;
    [sheet addSubview:title];

    UILabel *subtitle = [[UILabel alloc] init];
    subtitle.text = @"Enter name to allow temporary access via weblink to handle ongoing defects.";
    subtitle.font = [UIFont systemFontOfSize:14];
    subtitle.textAlignment = NSTextAlignmentCenter;
    subtitle.textColor = UIColor.darkGrayColor;
    subtitle.numberOfLines = 0;
    subtitle.lineBreakMode = NSLineBreakByWordWrapping;
    subtitle.translatesAutoresizingMaskIntoConstraints = NO;
    [sheet addSubview:subtitle];

  
    self.linkNameField = [[UITextField alloc] init];
    self.linkNameField.placeholder = @"Enter Link Name";
    self.linkNameField.borderStyle = UITextBorderStyleRoundedRect;
    [self.linkNameField addTarget:self
                           action:@selector(onTextChanged:)
                 forControlEvents:UIControlEventEditingChanged];
    self.linkNameField.translatesAutoresizingMaskIntoConstraints = NO;
    [sheet addSubview:self.linkNameField];
  
    UIButton *cancel = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancel setTitle:@"Cancel" forState:UIControlStateNormal];
    [cancel addTarget:self
               action:@selector(dismissBottomSheet)
     forControlEvents:UIControlEventTouchUpInside];
    [cancel setTitleColor:[UIColor colorWithRed:255.0/255.0
                                        green:100.0/255.0
                                         blue:100.0/255.0
                                        alpha:1.0]
                 forState:UIControlStateNormal];
    cancel.layer.cornerRadius = 10;
    cancel.layer.borderWidth = 0.5;
    cancel.layer.borderColor = [UIColor colorWithRed:255.0/255.0
                                               green:100.0/255.0
                                                blue:100.0/255.0
                                               alpha:1.0].CGColor;
    cancel.translatesAutoresizingMaskIntoConstraints = NO;
    [sheet addSubview:cancel];


    self.createButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.createButton setTitle:@"Update" forState:UIControlStateNormal];
    self.createButton.enabled = NO;
    self.createButton.backgroundColor = [UIColor colorWithRed:0.0 green:0.43 blue:0.55 alpha:1.0];
    self.createButton.layer.cornerRadius = 10;
    [self.createButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [self.createButton addTarget:self
                          action:@selector(onCreateLink)
                forControlEvents:UIControlEventTouchUpInside];
    self.createButton.translatesAutoresizingMaskIntoConstraints = NO;
    [sheet addSubview:self.createButton];


    [NSLayoutConstraint activateConstraints:@[
      [title.centerXAnchor constraintEqualToAnchor:sheet.centerXAnchor],
      [title.topAnchor constraintEqualToAnchor:sheet.topAnchor constant:28],
      
      [subtitle.topAnchor constraintEqualToAnchor:title.bottomAnchor constant:12],
      [subtitle.leadingAnchor constraintEqualToAnchor:sheet.leadingAnchor constant:16],
      [subtitle.trailingAnchor constraintEqualToAnchor:sheet.trailingAnchor constant:-16],
      
      [self.linkNameField.topAnchor constraintEqualToAnchor:subtitle.bottomAnchor constant:24],
      [self.linkNameField.leadingAnchor constraintEqualToAnchor:sheet.leadingAnchor constant:20],
      [self.linkNameField.trailingAnchor constraintEqualToAnchor:sheet.trailingAnchor constant:-20],
      [self.linkNameField.heightAnchor constraintEqualToConstant:48],
      
      [cancel.topAnchor constraintEqualToAnchor:self.linkNameField.bottomAnchor constant:32],
      [cancel.leadingAnchor constraintEqualToAnchor:subtitle.leadingAnchor],
      [cancel.trailingAnchor constraintEqualToAnchor:sheet.centerXAnchor constant:-6],
      [cancel.heightAnchor constraintEqualToConstant:48],
      
      [self.createButton.topAnchor constraintEqualToAnchor:self.linkNameField.bottomAnchor constant:32],
      [self.createButton.leadingAnchor constraintEqualToAnchor:sheet.centerXAnchor constant:6],
      [self.createButton.trailingAnchor constraintEqualToAnchor:subtitle.trailingAnchor],
      [self.createButton.heightAnchor constraintEqualToConstant:48],
      [self.createButton.bottomAnchor constraintEqualToAnchor:sheet.safeAreaLayoutGuide.bottomAnchor constant:-24],

    ]];
    return sheet;
}

- (void)onTextChanged:(UITextField *)textField {
    BOOL hasText = textField.text.length > 0;
    self.createButton.enabled = hasText;
    self.createButton.backgroundColor =
        hasText ? [UIColor colorWithRed:0.05 green:0.45 blue:0.57 alpha:1.0]
                : UIColor.systemGray5Color;
}

- (void)showAddLinkSheet {
    self.dimmedOverlay = [self buildDimmedOverlay];
    self.addLinkSheet = [self buildAddLinkSheet];

    [self.view addSubview:self.dimmedOverlay];
    [self.view addSubview:self.addLinkSheet];

    NSLayoutConstraint *bottom =
        [self.addLinkSheet.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:300];
    bottom.active = YES;

    [NSLayoutConstraint activateConstraints:@[
        [self.addLinkSheet.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [self.addLinkSheet.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [self.addLinkSheet.heightAnchor constraintEqualToConstant:320],
    ]];

    [self.view layoutIfNeeded];

    [UIView animateWithDuration:0.3 animations:^{
        self.dimmedOverlay.alpha = 1;
        bottom.constant = 0;
        [self.view layoutIfNeeded];
    }];
}

- (UIView *)buildShareSheet {
    UIView *sheet = [[UIView alloc] init];
    sheet.backgroundColor = UIColor.whiteColor;
    sheet.layer.cornerRadius = 24;
    sheet.layer.maskedCorners =
        kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
    sheet.translatesAutoresizingMaskIntoConstraints = NO;

    UILabel *title = [[UILabel alloc] init];
    title.text = @"Share 3rd Party Link URL";
    title.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    title.textAlignment = NSTextAlignmentCenter;
    title.translatesAutoresizingMaskIntoConstraints = NO;
    [sheet addSubview:title];

    UILabel *subtitle = [[UILabel alloc] init];
    subtitle.text = @"Select an option to share Issue Inspector’s link.";
    subtitle.font = [UIFont systemFontOfSize:14];
    subtitle.textColor = UIColor.darkGrayColor;
    subtitle.textAlignment = NSTextAlignmentCenter;
    subtitle.translatesAutoresizingMaskIntoConstraints = NO;
    [sheet addSubview:subtitle];

    UIButton *copyBtn = [self shareActionButton:@"Copy Link"
                                          image:@"doc.on.doc"
                                       selector:@selector(handleCopyLink)];

    UIButton *systemShareBtn = [self shareActionButton:@"Share via Apps"
                                                 image:@"square.and.arrow.up"
                                              selector:@selector(handleSystemShare)];

    UIButton *qrBtn = [self shareActionButton:@"Show QR Code"
                                        image:@"qrcode"
                                     selector:@selector(handleShowQR)];

    for (UIButton *btn in @[copyBtn, systemShareBtn, qrBtn]) {
        btn.translatesAutoresizingMaskIntoConstraints = NO;
        [sheet addSubview:btn];
    }

    [NSLayoutConstraint activateConstraints:@[
        [title.topAnchor constraintEqualToAnchor:sheet.topAnchor constant:28],
        [title.centerXAnchor constraintEqualToAnchor:sheet.centerXAnchor],

        [subtitle.topAnchor constraintEqualToAnchor:title.bottomAnchor constant:8],
        [subtitle.leadingAnchor constraintEqualToAnchor:sheet.leadingAnchor constant:20],
        [subtitle.trailingAnchor constraintEqualToAnchor:sheet.trailingAnchor constant:-20],

        [copyBtn.topAnchor constraintEqualToAnchor:subtitle.bottomAnchor constant:24],
        [copyBtn.leadingAnchor constraintEqualToAnchor:sheet.leadingAnchor constant:20],
        [copyBtn.trailingAnchor constraintEqualToAnchor:sheet.trailingAnchor constant:-20],
        [copyBtn.heightAnchor constraintEqualToConstant:48],

        [systemShareBtn.topAnchor constraintEqualToAnchor:copyBtn.bottomAnchor constant:12],
        [systemShareBtn.leadingAnchor constraintEqualToAnchor:copyBtn.leadingAnchor],
        [systemShareBtn.trailingAnchor constraintEqualToAnchor:copyBtn.trailingAnchor],
        [systemShareBtn.heightAnchor constraintEqualToConstant:48],

        [qrBtn.topAnchor constraintEqualToAnchor:systemShareBtn.bottomAnchor constant:12],
        [qrBtn.leadingAnchor constraintEqualToAnchor:copyBtn.leadingAnchor],
        [qrBtn.trailingAnchor constraintEqualToAnchor:copyBtn.trailingAnchor],
        [qrBtn.heightAnchor constraintEqualToConstant:48],

        [qrBtn.bottomAnchor constraintEqualToAnchor:sheet.safeAreaLayoutGuide.bottomAnchor constant:-24],
    ]];

    return sheet;
}

- (UIButton *)shareActionButton:(NSString *)title
                           image:(NSString *)systemImage
                        selector:(SEL)selector {

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.layer.cornerRadius = 10;
    btn.layer.borderWidth = 0.5;
    btn.layer.borderColor = [UIColor colorWithWhite:0.85 alpha:1.0].CGColor;
    btn.tintColor = [UIColor colorWithRed:0.05 green:0.45 blue:0.57 alpha:1.0];

    [btn setTitle:title forState:UIControlStateNormal];
    [btn setImage:[UIImage systemImageNamed:systemImage] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];

    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 16, 0, 0);
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 28, 0, 0);

    [btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];

    return btn;
}

- (void)dismissBottomSheet {
    [UIView animateWithDuration:0.25 animations:^{
        self.dimmedOverlay.alpha = 0;
        self.bottomSheet.transform = CGAffineTransformMakeTranslation(0, 300);
        self.addLinkSheet.transform = CGAffineTransformMakeTranslation(0, 300);
    } completion:^(BOOL finished) {
        [self.bottomSheet removeFromSuperview];
        [self.addLinkSheet removeFromSuperview];
        [self.dimmedOverlay removeFromSuperview];

        self.bottomSheet = nil;
        self.addLinkSheet = nil;
        self.dimmedOverlay = nil;
    }];
}

- (void)presentSuccessModal {
    self.successOverlay = [[UIView alloc] initWithFrame:self.view.bounds];
    self.successOverlay.backgroundColor =
        [[UIColor blackColor] colorWithAlphaComponent:0.6];

    self.successModal = [[UIView alloc] init];
    self.successModal.backgroundColor = UIColor.whiteColor;
    self.successModal.layer.cornerRadius = 24;
    self.successModal.translatesAutoresizingMaskIntoConstraints = NO;

    UILabel *title = [[UILabel alloc] init];
    title.text = @"Success";
    title.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    title.textAlignment = NSTextAlignmentCenter;
    title.translatesAutoresizingMaskIntoConstraints = NO;
  
    UILabel *subtitle = [[UILabel alloc] init];
    subtitle.text = @"3rd party link has been successfully updated.";
    subtitle.font = [UIFont systemFontOfSize:14];
    subtitle.textAlignment = NSTextAlignmentCenter;
    subtitle.textColor = UIColor.darkGrayColor;
    subtitle.numberOfLines = 0;
    subtitle.lineBreakMode = NSLineBreakByWordWrapping;
    subtitle.translatesAutoresizingMaskIntoConstraints = NO;

    UIButton *ok = [UIButton buttonWithType:UIButtonTypeSystem];
    [ok setTitle:@"OK" forState:UIControlStateNormal];
    [ok addTarget:self action:@selector(dismissSuccessModal) forControlEvents:UIControlEventTouchUpInside];
    [ok setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];

    ok.layer.cornerRadius = 10;
    ok.layer.backgroundColor = [UIColor colorWithRed:0.0 green:0.43 blue:0.55 alpha:1.0].CGColor;
    ok.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:self.successOverlay];
    [self.successOverlay addSubview:self.successModal];
    [self.successModal addSubview:title];
    [self.successModal addSubview:subtitle];
    [self.successModal addSubview:ok];

    [NSLayoutConstraint activateConstraints:@[
        [self.successModal.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
        [self.successModal.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor],
        [self.successModal.widthAnchor constraintEqualToConstant:312],
        [self.successModal.heightAnchor constraintEqualToConstant:309],
        
        [title.topAnchor constraintEqualToAnchor:self.successModal.topAnchor constant:18],
        [title.centerXAnchor constraintEqualToAnchor:self.successModal.centerXAnchor],
        
        [subtitle.topAnchor constraintEqualToAnchor:title.bottomAnchor constant:12],
        [subtitle.leadingAnchor constraintEqualToAnchor:self.successModal.leadingAnchor constant:20],
        [subtitle.trailingAnchor constraintEqualToAnchor:self.successModal.trailingAnchor constant:-20],
        
        [ok.topAnchor constraintEqualToAnchor:subtitle.bottomAnchor constant:32],
        [ok.centerXAnchor constraintEqualToAnchor:self.successModal.centerXAnchor],
        [ok.widthAnchor constraintEqualToConstant:166],
        [ok.heightAnchor constraintEqualToConstant:48],
//        [ok.bottomAnchor constraintEqualToAnchor:self.successModal.bottomAnchor constant:44],
    ]];

    self.successOverlay.alpha = 0;
    [UIView animateWithDuration:0.25 animations:^{
        self.successOverlay.alpha = 1;
    }];
}

- (void)dismissSuccessModal {
    [UIView animateWithDuration:0.2 animations:^{
        self.successOverlay.alpha = 0;
    } completion:^(BOOL finished) {
        [self.successOverlay removeFromSuperview];
        [self onBack];
    }];
}

- (void)presentDeleteConfirmModal {

    self.confirmOverlay = [[UIView alloc] initWithFrame:self.view.bounds];
    self.confirmOverlay.backgroundColor =
        [[UIColor blackColor] colorWithAlphaComponent:0.6];

    self.confirmModal = [[UIView alloc] init];
    self.confirmModal.backgroundColor = UIColor.whiteColor;
    self.confirmModal.layer.cornerRadius = 24;
    self.confirmModal.translatesAutoresizingMaskIntoConstraints = NO;

    UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_alert_orange"]];
    icon.translatesAutoresizingMaskIntoConstraints = NO;

    UILabel *title = [[UILabel alloc] init];
    title.text = @"Alert";
    title.font = [UIFont systemFontOfSize:18 weight:UIFontWeightBold];
    title.textAlignment = NSTextAlignmentCenter;
    title.translatesAutoresizingMaskIntoConstraints = NO;

    UILabel *message = [[UILabel alloc] init];
    message.text = @"Confirm to delete this 3rd party link?";
    message.font = [UIFont systemFontOfSize:14];
    message.textColor = UIColor.darkGrayColor;
    message.textAlignment = NSTextAlignmentCenter;
    message.numberOfLines = 0;
    message.translatesAutoresizingMaskIntoConstraints = NO;

    UIButton *cancel = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancel setTitle:@"Cancel" forState:UIControlStateNormal];
    cancel.layer.cornerRadius = 20;
    cancel.layer.borderWidth = 0.5;
    cancel.layer.borderColor = [UIColor colorWithRed:229/255.0
                                               green:57/255.0
                                                blue:53/255.0
                                               alpha:1].CGColor;
    [cancel setTitleColor:[UIColor colorWithRed:229/255.0
                                          green:57/255.0
                                           blue:53/255.0
                                          alpha:1]
                 forState:UIControlStateNormal];
    cancel.translatesAutoresizingMaskIntoConstraints = NO;
    [cancel addTarget:self
               action:@selector(dismissDeleteConfirmModal)
     forControlEvents:UIControlEventTouchUpInside];

    UIButton *confirm = [UIButton buttonWithType:UIButtonTypeSystem];
    [confirm setTitle:@"Confirm" forState:UIControlStateNormal];
    confirm.backgroundColor =
        [UIColor colorWithRed:0.0 green:0.43 blue:0.55 alpha:1.0];
    confirm.layer.cornerRadius = 20;
    [confirm setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    confirm.translatesAutoresizingMaskIntoConstraints = NO;
    [confirm addTarget:self
                action:@selector(confirmDeleteLink)
      forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.confirmOverlay];
    [self.confirmOverlay addSubview:self.confirmModal];

    [self.confirmModal addSubview:icon];
    [self.confirmModal addSubview:title];
    [self.confirmModal addSubview:message];
    [self.confirmModal addSubview:cancel];
    [self.confirmModal addSubview:confirm];

    [NSLayoutConstraint activateConstraints:@[
        [self.confirmModal.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
        [self.confirmModal.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor],
        [self.confirmModal.widthAnchor constraintEqualToConstant:309],
        [self.confirmModal.heightAnchor constraintEqualToConstant:295],

        [icon.topAnchor constraintEqualToAnchor:self.confirmModal.topAnchor constant:32],
        [icon.centerXAnchor constraintEqualToAnchor:self.confirmModal.centerXAnchor],
        [icon.widthAnchor constraintEqualToConstant:40],
        [icon.heightAnchor constraintEqualToConstant:40],

        [title.topAnchor constraintEqualToAnchor:icon.bottomAnchor constant:20],
        [title.centerXAnchor constraintEqualToAnchor:self.confirmModal.centerXAnchor],

        [message.topAnchor constraintEqualToAnchor:title.bottomAnchor constant:12],
        [message.leadingAnchor constraintEqualToAnchor:self.confirmModal.leadingAnchor constant:20],
        [message.trailingAnchor constraintEqualToAnchor:self.confirmModal.trailingAnchor constant:-20],

        [cancel.topAnchor constraintEqualToAnchor:message.bottomAnchor constant:32],
        [cancel.leadingAnchor constraintEqualToAnchor:self.confirmModal.leadingAnchor constant:20],
        [cancel.heightAnchor constraintEqualToConstant:48],

        [confirm.topAnchor constraintEqualToAnchor:message.bottomAnchor constant:32],
        [confirm.trailingAnchor constraintEqualToAnchor:self.confirmModal.trailingAnchor constant:-20],
        [confirm.leadingAnchor constraintEqualToAnchor:cancel.trailingAnchor constant:12],
        [confirm.widthAnchor constraintEqualToAnchor:cancel.widthAnchor],
        [confirm.heightAnchor constraintEqualToConstant:48],
    ]];

    self.confirmOverlay.alpha = 0;
    [UIView animateWithDuration:0.25 animations:^{
        self.confirmOverlay.alpha = 1;
    }];
}

- (void)dismissDeleteConfirmModal {
    [UIView animateWithDuration:0.2 animations:^{
        self.confirmOverlay.alpha = 0;
    } completion:^(BOOL finished) {
        [self.confirmOverlay removeFromSuperview];
        self.confirmModal = nil;
        self.confirmOverlay = nil;
    }];
}

- (void)confirmDeleteLink {
    [self dismissDeleteConfirmModal];
    [self handleDeleteLink];
}

#pragma mark - API
- (void)sendUpdatePayload:(NSDictionary *)payload
                 success:(void(^)(void))success
                    error:(void(^)(void))error{

    self.loadingOverlay.hidden = NO;

    [APIClient updateThirdPartyLink:payload
                         completion:^(NSDictionary *response, NSError *error) {

        self.loadingOverlay.hidden = YES;

        if (error || !response) {
            [self showAlert:@"Alert" message:@"Server error"];
            return;
        }

        NSLog(@"Update response: %@", response);

        if ([response[@"success"] boolValue]) {
            if (success) success();
        } else {
            NSString *message = response[@"message"] ?: @"Something went wrong";
            [self showAlert:@"Alert" message:message];
        }
    }];
}

- (void)handleToggleAccess:(UISwitch *)sender {
    // Capture previous value
    BOOL previousValue = !sender.isOn; // before API call, previous value is the opposite
    NSInteger status = sender.isOn ? 1 : 0;

    NSDictionary *payload = @{
        @"id": self.item[@"id"],
        @"status": @(status)
    };

    // Optimistically update the UI if you want, or leave as is
    // [self updateUI]; // optional

    [self sendUpdatePayload:payload success:^{
        // Success: keep switch as is
        [self updateUI];
    } error:^{
        // Failure: revert switch to previous value
        sender.on = previousValue;

        // Optional: show an alert
        [self showAlert:@"Error" message:@"Failed to update link access. Please try again."];
    }];
}

- (void)onCreateLink {
    if (self.linkNameField.text.length == 0) return;

    NSDictionary *payload = @{
        @"id": self.item[@"id"],
        @"title": self.linkNameField.text
    };

    [self sendUpdatePayload:payload success:^{
        [self dismissBottomSheet];
        [self presentSuccessModal];
    } error:^{
      [self showAlert:@"Error" message:@"Failed to update link name. Please try again."];
    }
    ];
}

- (void)handleDeleteLink {
    NSDictionary *payload = @{
        @"id": self.item[@"id"],
        @"status": @2
    };

    [self sendUpdatePayload:payload success:^{
        [self onBack];
    } error:^{
      [self showAlert:@"Error" message:@"Failed to delete link. Please try again."];
    }
    ];
}

- (void)handleSystemShare {
    [self dismissBottomSheet];

    NSString *fullURL =
        [NSString stringWithFormat:@"%@/%@",
         self.item[@"path"], self.item[@"url_code"]];

    UIActivityViewController *vc =
        [[UIActivityViewController alloc] initWithActivityItems:@[fullURL]
                                          applicationActivities:nil];
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)handleShowQR {
    [self dismissBottomSheet];
    [self.scrollView scrollRectToVisible:self.qrImageView.frame animated:YES];
}

@end
