import csv

def get_code_column_contents(input_filename):
    with open(input_filename, 'r', encoding='utf-8') as input_file:
        reader = csv.DictReader(input_file)
        code_column_contents = set()
        for row in reader:
            code_value = row['単元番号']  
            code_column_contents.add(code_value)
        return code_column_contents

def get_target_unit_numbers(input_filename, column_name, user_number):
    target_unit_numbers = set()
    
    with open(input_filename, 'r', encoding='utf-8') as input_file:
        reader = csv.DictReader(input_file)

        for row in reader:
            if row[user_number] == '1':
                target_unit_numbers.add(row[column_name])

    return target_unit_numbers

def process_csv(input_filename, output_filename, target_unit_numbers):
    with open(input_filename, 'r', encoding='utf-8') as input_file, \
         open(output_filename, 'w', encoding='utf-8', newline='') as output_file:
        
        reader = csv.DictReader(input_file)
        
        fieldnames = ['単元名', '単元番号', '指導時数', '前単元']
        writer = csv.DictWriter(output_file, fieldnames=fieldnames)
        writer.writeheader()

        previous_unit_numbers = set()
        code_contents = get_code_column_contents(input_filename)
        all_unit_numbers = set(code_contents)

        writer.writerow({
            '単元名': 'Start',
            '単元番号': '0',
            '指導時数': '0',
            '前単元': ''
        })

        for row in reader:
            unit_number = row['単元番号']

            # target_unit_numbersに含まれない単元番号の行は無視する
            if unit_number not in target_unit_numbers:
                continue

            unit_name = row['単元名']
            teaching_hours = row['指導時数']
            previous_units = row['前単元']

            if previous_units.lower() == 'nan':
                previous_units = ''

            if not previous_units:
                previous_units = '0'
            else:
                # カンマで分割し、かつ存在しない単元番号に対応する前単元の要素も削除
                previous_units = ','.join(num if num in target_unit_numbers else '0' for num in previous_units.split(','))

            writer.writerow({
                '単元名': unit_name,
                '単元番号': unit_number,
                '指導時数': teaching_hours,
                '前単元': previous_units
            })

            previous_unit_numbers.update(previous_units.split(','))

        valid_previous_units = previous_unit_numbers.intersection(target_unit_numbers)
        end_previous_units = ','.join(sorted(target_unit_numbers - valid_previous_units))

        writer.writerow({
            '単元名': 'End',
            '単元番号': '999',
            '指導時数': '0',
            '前単元': end_previous_units
        })

if __name__ == "__main__":
    input_schedule_filename = "schedule.csv"
    column_name_schedule = '単元番号'
    user_number_schedule = '1111'

    target_unit_numbers = get_target_unit_numbers(input_schedule_filename, column_name_schedule, user_number_schedule)
    print("Target Unit Numbers:", target_unit_numbers)

    input_ori_filename = "ori.csv"
    output_filename = "float.csv"

    process_csv(input_ori_filename, output_filename, target_unit_numbers)
