import os
from pypdf import PdfReader, PdfWriter

# Your defined paths
pdf_path = r"C:\Users\anjal\OneDrive\Documents\Desktop\MSc_CS\Revised-MSc-Syllabus_June-2024.pdf"
output_folder = r"C:\Users\anjal\OneDrive\Documents\Desktop\MSc_CS\DSO"
# Your defined ranges (1-based index)
ranges = [

    (30, 31, "MCSO301"),
    (31, 32, "MCSO302"),
]


def split_pdf(input_pdf, output_dir, page_ranges):
    # Create output directory if it doesn't exist
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
        print(f"Created output folder: {output_dir}")

    # Load the PDF
    reader = PdfReader(input_pdf)
    total_pages = len(reader.pages)
    print(f"Loaded PDF with {total_pages} pages.\nStarting split...")

    for start, end, filename in page_ranges:
        writer = PdfWriter()

        # Convert 1-based human input to 0-based Python indexing
        # end is inclusive, so we go up to end - 1 + 1 (which is just end)
        start_idx = start - 1
        end_idx = end

        # Validation bounds check
        if start_idx < 0 or end_idx > total_pages:
            print(
                f"⚠️ Error: Range {start}-{end} for {filename} is out of bounds. Skipping.")
            continue

        # Add the specified pages to the writer
        for page_num in range(start_idx, end_idx):
            writer.add_page(reader.pages[page_num])

        # Define final output path
        output_filename = os.path.join(output_dir, f"{filename}.pdf")

        # Save the file
        with open(output_filename, "wb") as output_pdf:
            writer.write(output_pdf)

        print(f"Successfully created: {filename}.pdf (Pages {start} to {end})")


if __name__ == "__main__":
    split_pdf(pdf_path, output_folder, ranges)
    print("\n All PDF segments have been generated successfully!")
