import Series from 'App/Models/Series'
import Episode from 'App/Models/Episode'
import BaseSeeder from '@ioc:Adonis/Lucid/Seeder'
import { readdirSync, readFileSync } from 'fs'
import { join } from 'path'
import Decimal from 'decimal.js'
export default class extends BaseSeeder {
    public async run() {
        // Write your database queries inside the run method
        const dir = '/Volumes/Data HD/shortplay/downloaded/'
        for (let file of readdirSync(dir)
            .filter((file) => /^[^\.].*\.json$/.test(file))
            .sort()
            .splice(0, 10)) {
            const data = JSON.parse(readFileSync(join(dir, file)).toString())
            console.log(data.video.title)
            const series = await Series.create({
                title: data.video.title,
                cover: data.video.img,
                price: new Decimal('999'),
                totalEpisodes: data.video.total
            })

            let i = 0
            const eps: Episode[] = []
            for (let s of data.series) {
                const ep = new Episode()
                ep.merge({
                    seriesId: series.id,
                    episodeNum: s.sn,
                    video: s.path,
                    price: ++i > 10 ? new Decimal('99') : new Decimal('0')
                })
                eps.push(ep)
            }
            await Episode.createMany(eps)
            console.log(data.video.id)
        }
    }
}
