// PlanViewController.mm
#import "PlanViewController.h"
#import "DetailsViewController.h"

@interface PlanViewController ()

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UIButton *helpButton;

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *planImageView;

@end

@implementation PlanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    NSLog(@"PlanViewController loaded");

    // Create UI elements (but don’t assign final frames yet)
    [self createHeader];
    [self createScrollView];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    // Get safe area + bounds
    CGFloat topInset = self.view.safeAreaInsets.top;
    CGFloat headerHeight = 48.0;

    // Layout header
    self.headerView.frame = CGRectMake(0, topInset, self.view.bounds.size.width, headerHeight);

    // Layout title
    self.titleLabel.frame = CGRectMake(0, 8, self.headerView.bounds.size.width, 32);
    self.helpButton.frame = CGRectMake(self.headerView.bounds.size.width - 44, 8, 32, 32);
    self.backButton.frame = CGRectMake(12, 8, 32, 32);

    // Layout scroll view
    CGFloat scrollY = topInset + headerHeight;
    self.scrollView.frame = CGRectMake(0, scrollY, self.view.bounds.size.width,
                                       self.view.bounds.size.height - scrollY);

    self.planImageView.frame = self.scrollView.bounds;

    // Make absolutely sure header is above scroll view
    [self.view bringSubviewToFront:self.headerView];
}

#pragma mark - UI Creation

- (void)createHeader {
    self.headerView = [[UIView alloc] init];
    self.headerView.backgroundColor = [UIColor colorWithRed:0/255.0 green:109/255.0 blue:141/255.0 alpha:1];
    [self.view addSubview:self.headerView];
    
    // Back button
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setImage:[UIImage imageNamed:@"back24"] forState:UIControlStateNormal];
    [self.backButton addTarget:self action:@selector(handleBack) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:self.backButton];
    
    // Title
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"Plan Viewer";
    self.titleLabel.textColor = UIColor.whiteColor;
    self.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.headerView addSubview:self.titleLabel];
    
    // Help button
    self.helpButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.helpButton setImage:[UIImage imageNamed:@"btn_white_help_3x"] forState:UIControlStateNormal];
    [self.helpButton addTarget:self action:@selector(showTutorial) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:self.helpButton];
}

- (void)createScrollView {
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.delegate = (id<UIScrollViewDelegate>)self;
    self.scrollView.minimumZoomScale = 1.0;
    self.scrollView.maximumZoomScale = 5.0;
    [self.view addSubview:self.scrollView];
    
    self.planImageView = [[UIImageView alloc] init];
    self.planImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.scrollView addSubview:self.planImageView];

    UIImage *testImage = [UIImage imageNamed:@"image_placeholder"];
    if (!testImage) {
        NSLog(@"❌ image_placeholder not found in bundle!");
        self.planImageView.backgroundColor = [UIColor systemTealColor];

        // 👉 Temporary test button
        UIButton *testButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [testButton setTitle:@"Next Screen →" forState:UIControlStateNormal];
        testButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        [testButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        testButton.backgroundColor = [UIColor colorWithRed:0.0 green:0.43 blue:0.55 alpha:1.0];
        testButton.layer.cornerRadius = 8;
        testButton.clipsToBounds = YES;

        // Add simple frame — will center it later in viewDidLayoutSubviews
        testButton.frame = CGRectMake(0, 0, 200, 50);
        testButton.center = self.view.center;

        [testButton addTarget:self
                       action:@selector(handleNextScreen)
             forControlEvents:UIControlEventTouchUpInside];

        // Add directly on top of main view (not inside scrollView)
        [self.view addSubview:testButton];
    } else {
        self.planImageView.image = testImage;
    }
}

- (void)handleNextScreen {
    NSLog(@"➡️ Test button tapped — navigating to AddIssueDetailsViewController");

    AddIssueDetailsViewController *nextVC = [[AddIssueDetailsViewController alloc] init];
    nextVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nextVC animated:YES completion:nil];
}

#pragma mark - Actions

- (void)handleBack {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)showTutorial {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Help"
                                                                   message:@"This would show the tutorial (e.g. step1/step2/step3)."
                                                            preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.planImageView;
}

@end
