CREATE TABLE IF NOT EXISTS "iafd_performers" (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    gender TEXT,
    birthday TEXT,
    astrology TEXT,
    birthplace TEXT,
    years_active TEXT,
    ethnicity TEXT,
    nationality TEXT,
    hair_colors TEXT, 
    eye_color TEXT,
    height_str TEXT,
    weight_str TEXT,  
    measurements TEXT,
    tattoos TEXT,
    piercings TEXT,  
    fake_tits TEXT,
    href TEXT UNIQUE,  
    created_at TEXT DEFAULT (datetime('now', 'localtime')),
    updated_at TEXT DEFAULT (datetime('now', 'localtime')),
    weight INTEGER, 
    height INTEGER,
    rating INTEGER, 
    movies_cnt INTEGER,
    vixen_cnt INTEGER,
    blacked_cnt INTEGER,
    tushy_cnt INTEGER,
    x_art_cnt INTEGER, 
    is_full_data INTEGER DEFAULT (0) NOT NULL, 
    birth_year INTEGER DEFAULT (0) NOT NULL, 
    from_astro_list INTEGER DEFAULT (0) NOT NULL, 
    from_birth_list INTEGER DEFAULT (0) NOT NULL, 
    from_ethnic_list INTEGER DEFAULT (0) NOT NULL,
    from_movie_list INTEGER DEFAULT (0) NOT NULL
  );
CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE IF NOT EXISTS "iafd_performer_aliases" (
  `performer_id` integer NOT NULL,
  `alias` varchar(255) NOT NULL,
  foreign key(`performer_id`) references "iafd_performers"(`id`) on delete CASCADE,
  PRIMARY KEY(`performer_id`, `alias`)
);
CREATE TABLE IF NOT EXISTS "iafd_movies_appers_in" (
  `movie_id` integer,
  `appears_in_id` integer,
  `gradation` integer,  
  `notes` varchar(255),  
  foreign key(`movie_id`) references "iafd_movies"(`id`) on delete CASCADE,
  foreign key(`appears_in_id`) references "iafd_movies"(`id`) on delete CASCADE,
  PRIMARY KEY (`movie_id`, `appears_in_id`)
);
CREATE TABLE IF NOT EXISTS "iafd_movies" (
  `id` integer not null primary key autoincrement,
  `title` varchar(255),
  `minutes` varchar(255),
  `distributor_id` integer,
  `studio_id` integer,
  `release_date` varchar(255),
  `added_to_IAFD_date` varchar(255),
  `all_girl` varchar(255),
  `all_male` varchar(255),
  `compilation` varchar(255),
  `webscene` varchar(255),
  `director_id` integer,
  `href` varchar(255) UNIQUE,
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime')), 
  is_full_data INTEGER DEFAULT (0) NOT NULL, 
  release_year INTEGER DEFAULT (0) NOT NULL, 
  from_performer_list INTEGER DEFAULT (0) NOT NULL, 
  from_dist_list INTEGER DEFAULT (0) NOT NULL, 
  from_stu_list INTEGER DEFAULT (0) NOT NULL,  
  foreign key(`studio_id`) references "iafd_studios"(`id`) on delete SET NULL,
  foreign key(`distributor_id`) references "iafd_distributors"(`id`) on delete SET NULL  
);
CREATE TABLE IF NOT EXISTS "iafd_performers_movies" (
  `performer_id` integer,
  `movie_id` integer,
  `role` varchar(255),
  `notes` varchar(255),  
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  foreign key(`performer_id`) references "iafd_performers"(`id`) on delete CASCADE,
  foreign key(`movie_id`) references "iafd_movies"(`id`) on delete CASCADE,
  PRIMARY KEY (`movie_id`, `performer_id`)
);
CREATE TABLE IF NOT EXISTS "iafd_studios" (
  `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `href` VARCHAR(255) UNIQUE,
  `parent_id` INTEGER DEFAULT NULL CHECK (`id` IS NOT `parent_id`) REFERENCES "iafd_studios"(`id`) ON DELETE SET NULL,
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `details` TEXT
);
CREATE TABLE IF NOT EXISTS "iafd_distributors" (
  `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `href` VARCHAR(255) UNIQUE,
  `parent_id` INTEGER DEFAULT NULL CHECK (`id` IS NOT `parent_id`) REFERENCES "iafd_distributors"(`id`) ON DELETE SET NULL,
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `details` TEXT
);
CREATE TABLE IF NOT EXISTS "iafd_performer_urls" (
  `performer_id` integer NOT NULL,
  `position` varchar(255) NOT NULL,
  `url` varchar(255) NOT NULL,
  foreign key(`performer_id`) references "iafd_performers"(`id`) on delete CASCADE,
  PRIMARY KEY(`performer_id`, `position`, `url`)
);
CREATE TABLE IF NOT EXISTS "iafd_task_log" (
  `task_id` integer not null primary key autoincrement,
  `full_data_performers` integer,
  `total_performers` integer,
  `full_data_movies` integer,
  `total_movies` integer,
  `total_distributors` integer,
  `total_studios` integer,
  `task_status` varchar(255),
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime'))
);
CREATE TABLE IF NOT EXISTS "iafd_meta_ethnic" (
  `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `href` VARCHAR(255) UNIQUE,
  `created_at` TEXT DEFAULT (datetime('now', 'localtime'))
);
CREATE TABLE javhd_models (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    rank INTEGER,
    ja_name TEXT,
    zh_name TEXT,
    en_name TEXT,
    url TEXT UNIQUE,
    pic TEXT,
    height TEXT,
    weight TEXT,
    breast_size TEXT,
    breast_factor TEXT,
    hair_color TEXT,
    eye_color TEXT,
    birth_date TEXT,
    ethnicity TEXT,
    birth_place TEXT,
    created_at TEXT DEFAULT (datetime('now', 'localtime')),
    updated_at TEXT DEFAULT (datetime('now', 'localtime'))
, is_full_data INTEGER DEFAULT (0) NOT NULL);
CREATE TABLE thelordofporn_actress (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            pornstar TEXT,
            rating REAL,
            rank INTEGER,
            votes INTEGER,
            href TEXT UNIQUE,
            career_start TEXT,
            measurements TEXT,
            born TEXT,
            height TEXT,
            weight TEXT,
            date_modified TEXT,
            global_rank INTEGER,
            weekly_rank INTEGER,
            last_month_rating REAL,
            current_rating REAL,
            total_votes INTEGER,
            birth_date TEXT,
            birth_year TEXT,
            birth_place TEXT,
            height_ft TEXT,
            height_cm TEXT,
            weight_lbs TEXT,
            weight_kg TEXT,
            created_at TEXT DEFAULT (datetime('now', 'localtime')),
            updated_at TEXT DEFAULT (datetime('now', 'localtime'))
        , is_full_data INTEGER DEFAULT (0) NOT NULL);
CREATE TABLE thelordofporn_alias (
            actress_id INTEGER NOT NULL,
            alias TEXT NOT NULL, updated_at TEXT,
            FOREIGN KEY (actress_id) REFERENCES thelordofporn_actress(id) ON DELETE CASCADE,
            PRIMARY KEY(`actress_id`, `alias`)
        );
CREATE TABLE javdb_actors (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL,
            href TEXT UNIQUE NOT NULL,
            pic TEXT,
            created_at DATETIME DEFAULT (datetime('now', 'localtime')),
            updated_at DATETIME DEFAULT (datetime('now', 'localtime')), 
            is_full_data INTEGER DEFAULT (0) NOT NULL, 
            from_actor_list INTEGER DEFAULT (0) NOT NULL, 
            from_movie_list INTEGER DEFAULT (0) NOT NULL
);
CREATE TABLE javdb_actors_alias (
            actor_id INTEGER NOT NULL,
            alias TEXT NOT NULL,
            created_at DATETIME DEFAULT (datetime('now', 'localtime')),
            updated_at DATETIME DEFAULT (datetime('now', 'localtime')),
            PRIMARY KEY (actor_id, alias),
            FOREIGN KEY (actor_id) REFERENCES javdb_actors(id) ON DELETE CASCADE
        );
CREATE TABLE IF NOT EXISTS "javdb_makers" (
  `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `href` VARCHAR(255) UNIQUE,
  `parent_id` INTEGER DEFAULT NULL CHECK (`id` IS NOT `parent_id`) REFERENCES "javdb_makers"(`id`) ON DELETE SET NULL,
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `details` TEXT
, from_list INTEGER DEFAULT (0), from_movie_list INTEGER DEFAULT (0));
CREATE TABLE IF NOT EXISTS "javdb_series" (
  `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `href` VARCHAR(255) UNIQUE,
  `parent_id` INTEGER DEFAULT NULL CHECK (`id` IS NOT `parent_id`) REFERENCES "javdb_series"(`id`) ON DELETE SET NULL,
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `details` TEXT
, from_list INTEGER DEFAULT (0), from_movie_list INTEGER DEFAULT (0));
CREATE TABLE IF NOT EXISTS "javdb_movies" (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            href TEXT UNIQUE,
            title TEXT,
            cover_url TEXT,
            serial_number TEXT,
            release_date TEXT,
            duration TEXT,
            maker_id TEXT,
            series_id TEXT,
            is_full_data INTEGER DEFAULT (0) NOT NULL, 
            created_at TEXT DEFAULT (datetime('now', 'localtime')),
            updated_at TEXT DEFAULT (datetime('now', 'localtime')), 
            from_actor_list INTEGER DEFAULT (0) NOT NULL, 
            from_movie_makers INTEGER DEFAULT (0) NOT NULL, 
            from_movie_series INTEGER DEFAULT (0) NOT NULL
, from_movie_publishers INTEGER DEFAULT (0) NOT NULL, pub_id INTEGER, uncensored INTEGER DEFAULT (0) NOT NULL);
CREATE TABLE IF NOT EXISTS "javdb_actors_movies" (
            actor_id INTEGER,
            movie_id INTEGER,
            tags TEXT,
            created_at TEXT DEFAULT (datetime('now', 'localtime')), updated_at TEXT,
            PRIMARY KEY (actor_id, movie_id),
            FOREIGN KEY (actor_id) REFERENCES javdb_actors(id),
            FOREIGN KEY (movie_id) REFERENCES "javdb_movies"(id)
);
CREATE TABLE IF NOT EXISTS "javdb_task_log" (
  `task_id` integer not null primary key autoincrement,
  `full_data_actors` integer,
  `total_actors` integer,
  `full_data_movies` integer,
  `total_movies` integer,
  `total_makers` integer,
  `total_series` integer,
  `task_status` varchar(255),
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime'))
);
CREATE VIEW view_iafd_movies_summary AS
SELECT 
	COUNT(*) AS total_count, 
    SUM(CASE WHEN from_performer_list = 1 THEN 1 ELSE 0 END) AS from_perfromers,
    SUM(CASE WHEN from_dist_list = 1 THEN 1 ELSE 0 END) AS from_dis,
    SUM(CASE WHEN from_stu_list = 1 THEN 1 ELSE 0 END) AS from_stu,    
    SUM(CASE WHEN from_performer_list=1 AND from_dist_list = 1 THEN 1 ELSE 0 END) AS performers_dist,
    SUM(CASE WHEN from_performer_list=1 AND from_stu_list = 1 THEN 1 ELSE 0 END) AS performers_stu,
    SUM(CASE WHEN from_dist_list=1 AND from_stu_list = 1 THEN 1 ELSE 0 END) AS dist_stu,    
    SUM(CASE WHEN from_performer_list=1 AND from_dist_list=1 AND from_stu_list = 1 THEN 1 ELSE 0 END) AS performers_dist_stu, 
    SUM(CASE WHEN from_performer_list=1 AND from_dist_list=0 AND from_stu_list = 0 THEN 1 ELSE 0 END) AS performers_only,
    SUM(CASE WHEN from_performer_list=0 AND from_dist_list=1 AND from_stu_list = 0 THEN 1 ELSE 0 END) AS dist_only,
    SUM(CASE WHEN from_performer_list=0 AND from_dist_list=0 AND from_stu_list = 1 THEN 1 ELSE 0 END) AS stu_only
FROM iafd_movies im
/* view_iafd_movies_summary(total_count,from_perfromers,from_dis,from_stu,performers_dist,performers_stu,dist_stu,performers_dist_stu,performers_only,dist_only,stu_only) */;
CREATE VIEW view_iafd_thelordofporn_match AS
SELECT 
    ia.id AS iafd_id, ia.href AS iafd_href, ia.name AS iafd_name,
    tl.id AS tl_id, tl.pornstar AS tl_pornstar, tl.href AS tl_href
FROM thelordofporn_actress tl
JOIN iafd_performers ia ON tl.pornstar = ia.name
/* view_iafd_thelordofporn_match(iafd_id,iafd_href,iafd_name,tl_id,tl_pornstar,tl_href) */;
CREATE VIEW view_iafd_performers_movies AS
SELECT p.id, p.href, p.name, IFNULL(COUNT(pm.performer_id), 0) AS actual_movies_cnt, p.movies_cnt
FROM iafd_performers p
LEFT JOIN iafd_performers_movies pm ON pm.performer_id = p.id
GROUP BY p.id
/* view_iafd_performers_movies(id,href,name,actual_movies_cnt,movies_cnt) */;
CREATE VIEW view_javdb_javhd_match AS
SELECT 
    ja.id AS javdb_id, ja.href AS javdb_href, ja.name AS javdb_name,
    jm.id AS javhd_id, jm.ja_name AS javhd_ja_name, jm.en_name AS javhd_en_name, jm.url AS javhd_url
FROM javdb_actors ja 
JOIN javhd_models jm ON ja.name = jm.ja_name
/* view_javdb_javhd_match(javdb_id,javdb_href,javdb_name,javhd_id,javhd_ja_name,javhd_en_name,javhd_url) */;
CREATE VIEW view_iafd_javdb_javhd_match AS
SELECT 
	ip.id AS iafd_id, ip.href AS iafd_href, ip.name AS iafd_name,
	jjm.javdb_id AS javdb_id, jjm.javdb_name AS javdb_name, jjm.javdb_href AS javdb_href,
	jjm.javhd_id AS javhd_id, jjm.javhd_en_name AS javhd_en_name, jjm.javhd_url AS javhd_url 
FROM iafd_performers ip 
JOIN javdb_javhd_match jjm ON ip.name = jjm.javhd_en_name;
CREATE TABLE IF NOT EXISTS "javdb_tags" (
  `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `href` VARCHAR(255) UNIQUE,
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime'))
);
CREATE TABLE IF NOT EXISTS "javdb_movies_tags" (
            movie_id INTEGER,
            tag_id INTEGER,
            tags TEXT,
            created_at TEXT DEFAULT (datetime('now', 'localtime')), 
            updated_at TEXT DEFAULT (datetime('now', 'localtime')),
            PRIMARY KEY (tag_id, movie_id),
            FOREIGN KEY (tag_id) REFERENCES javdb_tags(id),
            FOREIGN KEY (movie_id) REFERENCES "javdb_movies"(id)
);
CREATE VIEW view_javdb_stat_actors AS
SELECT
    is_full_data,
    from_actor_list,
    COUNT(*) AS record_count,
    (SELECT COUNT(*) FROM javdb_actors) AS total_record_count
FROM
    javdb_actors
GROUP BY
    is_full_data, from_actor_list
/* view_javdb_stat_actors(is_full_data,from_actor_list,record_count,total_record_count) */;
CREATE VIEW view_javdb_stat_movie_maker_series AS
-- 统计 makers 相关数据
SELECT 
  'makers' AS stat_type,
  COALESCE(m.id, 0) AS id,  -- 如果 m.id 为 NULL，使用 -1 作为默认值
  COALESCE(m.name, '') AS name,
  COALESCE(m.href, '') AS href,
  COALESCE(m.from_list, 0) AS from_list,
  COALESCE(m.from_movie_list, 0) AS from_movie_list,
  COUNT(mv.id) AS count
FROM javdb_movies mv
LEFT JOIN javdb_makers m ON mv.maker_id = m.id
GROUP BY mv.maker_id
UNION ALL
SELECT 
  'series' AS stat_type,
  COALESCE(s.id, 0) AS id,  -- 如果 s.id 为 NULL，使用 -1 作为默认值
  COALESCE(s.name, '') AS name,
  COALESCE(s.href, '') AS href,
  COALESCE(s.from_list, 0) AS from_list,
  COALESCE(s.from_movie_list, 0) AS from_movie_list,
  COUNT(mv.id) AS count
FROM javdb_movies mv
LEFT JOIN javdb_series s ON mv.series_id = s.id
GROUP BY mv.series_id
ORDER BY count DESC
/* view_javdb_stat_movie_maker_series(stat_type,id,name,href,from_list,from_movie_list,count) */;
CREATE VIEW view_javdb_stat_movie_by_year AS
SELECT 
    -- 提取年份，如果日期不符合格式或者为空，当作 0000-00-00 处理
	substr(release_date, 1, 4) AS release_year,
    -- 统计每年的影片数量
    COUNT(*) AS movie_count
FROM 
    javdb_movies
GROUP BY 
    release_year
ORDER BY 
    release_year
/* view_javdb_stat_movie_by_year(release_year,movie_count) */;
CREATE TABLE IF NOT EXISTS "javdb_publishers" (
  `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `href` VARCHAR(255) UNIQUE,
  `parent_id` INTEGER DEFAULT NULL CHECK (`id` IS NOT `parent_id`) REFERENCES "javdb_makers"(`id`) ON DELETE SET NULL,
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `details` TEXT, 
  `from_list` INTEGER DEFAULT (0), 
  `from_movie_list` INTEGER DEFAULT (0)
);
CREATE VIEW view_javdb_stat_movies AS
SELECT
    is_full_data,
    uncensored,
    COUNT(*) AS record_count,
    (SELECT COUNT(*) FROM javdb_movies) AS total_record_count
FROM
    javdb_movies
GROUP BY
    is_full_data, uncensored
/* view_javdb_stat_movies(is_full_data,uncensored,record_count,total_record_count) */;
CREATE VIEW view_javdb_stat_movies_uncensored AS
-- 第一种统计：筛选javdb_movies表中，其在javdb_actors_movies中对应的actor_id，在javdb_actors中的from_actor_list =1
SELECT 
    'actor_list' AS stat_type,
    jm.id,
    jm.href,
    jm.title,
    jm.serial_number,
    jm.release_date,
    jm.is_full_data,
    jm.uncensored
FROM 
    javdb_movies jm
JOIN 
    javdb_actors_movies jam ON jm.id = jam.movie_id
JOIN 
    javdb_actors ja ON jam.actor_id = ja.id
WHERE 
    ja.from_actor_list = 1
UNION ALL
-- 第二种统计：筛选javdb_movies表中，其maker_id不为空且在javdb_makers中对应的行的from_list = 1
SELECT 
   'maker_list' AS stat_type,
    jm.id,
    jm.href,
    jm.title,
    jm.serial_number,
    jm.release_date,
    jm.is_full_data,
    jm.uncensored
FROM 
    javdb_movies jm
JOIN 
    javdb_makers jmk ON jm.maker_id = jmk.id
WHERE 
    jm.maker_id IS NOT NULL AND jmk.from_list = 1
UNION ALL
-- 第三种统计：筛选javdb_movies表中，其series_id不为空且在javdb_series中对应的行的from_list = 1
SELECT 
   'series_list' AS stat_type,
    jm.id,
    jm.href,
    jm.title,
    jm.serial_number,
    jm.release_date,
    jm.is_full_data,
    jm.uncensored
FROM 
    javdb_movies jm
JOIN 
    javdb_series js ON jm.series_id = js.id
WHERE 
    jm.series_id IS NOT NULL AND js.from_list = 1
/* view_javdb_stat_movies_uncensored(stat_type,id,href,title,serial_number,release_date,is_full_data,uncensored) */;
CREATE TABLE javbus_actors (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    ja_name TEXT,
    zh_name TEXT,
    en_name TEXT,
    href TEXT UNIQUE,
    pic TEXT,
    birth_date TEXT,
    height TEXT,
    breast_size TEXT,
    measurements TEXT,
    created_at TEXT DEFAULT (datetime('now', 'localtime')),
    updated_at TEXT DEFAULT (datetime('now', 'localtime')), 
    uncensored INTEGER DEFAULT (0) NOT NULL, 
    is_full_data INTEGER DEFAULT (0) NOT NULL, 
	from_actor_list INTEGER DEFAULT (0) NOT NULL, 
	from_movie_list INTEGER DEFAULT (0) NOT NULL
, movies_cnt INTEGER DEFAULT 0 NOT NULL);
CREATE TABLE IF NOT EXISTS "javbus_movies" (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            href TEXT UNIQUE,
            title TEXT,
            cover_url TEXT,
            serial_number TEXT,
            release_date TEXT,
            duration TEXT,
            studio_id INTEGER,
            label_id INTEGER,
            series_id INTEGER,
            is_full_data INTEGER DEFAULT (0) NOT NULL, 
            uncensored INTEGER DEFAULT (0) NOT NULL, 
            created_at TEXT DEFAULT (datetime('now', 'localtime')),
            updated_at TEXT DEFAULT (datetime('now', 'localtime')), 
            from_actor_list INTEGER DEFAULT (0) NOT NULL, 
            from_movie_studios INTEGER DEFAULT (0) NOT NULL, 
            from_movie_labels INTEGER DEFAULT (0) NOT NULL, 
            from_movie_series INTEGER DEFAULT (0) NOT NULL
, actors_cnt INTEGER DEFAULT (0) NOT NULL);
CREATE TABLE IF NOT EXISTS "javbus_actors_movies" (
            actor_id INTEGER,
            movie_id INTEGER,
            tags TEXT,
            created_at TEXT DEFAULT (datetime('now', 'localtime')), 
            updated_at TEXT DEFAULT (datetime('now', 'localtime')),
            PRIMARY KEY (actor_id, movie_id),
            FOREIGN KEY (actor_id) REFERENCES javbus_actors(id),
            FOREIGN KEY (movie_id) REFERENCES "javbus_movies"(id)
);
CREATE TABLE IF NOT EXISTS "javbus_labels" (
  `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255),
  `en_name` VARCHAR(255),
  `ja_name` VARCHAR(255),
  `href` VARCHAR(255) UNIQUE,
  `details` TEXT, 
  `uncensored` INTEGER DEFAULT (0),    
  `from_list` INTEGER DEFAULT (0), 
  `from_movie_list` INTEGER DEFAULT (0),
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime'))
 , movies_cnt INTEGER DEFAULT (0), magnet_cnt INTEGER DEFAULT (0));
CREATE TABLE IF NOT EXISTS "javbus_series" (
  `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255),
  `en_name` VARCHAR(255),
  `ja_name` VARCHAR(255),
  `href` VARCHAR(255) UNIQUE,
  `details` TEXT, 
  `uncensored` INTEGER DEFAULT (0),    
  `from_list` INTEGER DEFAULT (0), 
  `from_movie_list` INTEGER DEFAULT (0),
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime'))
 , movies_cnt INTEGER DEFAULT (0), magnet_cnt INTEGER DEFAULT (0));
CREATE TABLE IF NOT EXISTS "javbus_studios" (
  `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255),
  `en_name` VARCHAR(255),
  `ja_name` VARCHAR(255),
  `href` VARCHAR(255) UNIQUE,
  `details` TEXT, 
  `uncensored` INTEGER DEFAULT (0),    
  `from_list` INTEGER DEFAULT (0), 
  `from_movie_list` INTEGER DEFAULT (0),
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime'))
 , movies_cnt INTEGER DEFAULT (0), magnet_cnt INTEGER DEFAULT (0));
CREATE TABLE IF NOT EXISTS "javbus_tags" (
  `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255),
  `en_name` VARCHAR(255),
  `ja_name` VARCHAR(255),
  `href` VARCHAR(255) UNIQUE,
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime'))
);
CREATE TABLE IF NOT EXISTS "javbus_movies_tags" (
            movie_id INTEGER,
            tag_id INTEGER,
            tags TEXT,
            created_at TEXT DEFAULT (datetime('now', 'localtime')), 
            updated_at TEXT DEFAULT (datetime('now', 'localtime')),
            PRIMARY KEY (tag_id, movie_id),
            FOREIGN KEY (tag_id) REFERENCES javbus_tags(id),
            FOREIGN KEY (movie_id) REFERENCES "javbus_movies"(id)
);
CREATE INDEX idx_actor_movie_actor_id 
                ON javbus_actors_movies(actor_id);
CREATE INDEX idx_iafd_performers_movies_performer_id
            ON iafd_performers_movies(performer_id);
CREATE TABLE IF NOT EXISTS "pbox_studios" (
  `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `href` VARCHAR(255) UNIQUE,
  `label_id` INTEGER NOT NULL DEFAULT 0,
  `scene_count` INTEGER NOT NULL DEFAULT 0,
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `description` TEXT
);
CREATE TABLE IF NOT EXISTS "pbox_movies" (
  `id` integer not null primary key autoincrement,
  `href` varchar(255) UNIQUE,
  `title` varchar(255),
  `movie_id` integer,
  `content_id` integer,
  `duration` varchar(255),
  `publish_date` varchar(255),
  `release_date` varchar(255),
  `studio_id` integer,
  `is_full_data` INTEGER DEFAULT (0) NOT NULL, 
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime')), 
  foreign key(`studio_id`) references "pbox_studios"(`id`) on delete SET NULL
);
CREATE TABLE IF NOT EXISTS "pbox_tags" (
  `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `href` VARCHAR(255) UNIQUE,
  `tag_id` INTEGER,
  `created_at` TEXT DEFAULT (datetime('now', 'localtime')),
  `updated_at` TEXT DEFAULT (datetime('now', 'localtime'))
);
CREATE TABLE IF NOT EXISTS "pbox_actors" (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    href TEXT UNIQUE, 
    gender TEXT,
    age INTEGER,
    nationality TEXT,
    country TEXT,   
    movies_cnt INTEGER DEFAULT (0) NOT NULL, 
    is_full_data INTEGER DEFAULT (0) NOT NULL, 
    created_at TEXT DEFAULT (datetime('now', 'localtime')),
    updated_at TEXT DEFAULT (datetime('now', 'localtime'))
  );
CREATE TABLE IF NOT EXISTS "pbox_actor_aliases" (
  `actor_id` integer NOT NULL,
  `alias` varchar(255) NOT NULL,
  foreign key(`actor_id`) references "pbox_actors"(`id`) on delete CASCADE,
  PRIMARY KEY(`actor_id`, `alias`)
);
CREATE TABLE IF NOT EXISTS "pbox_movies_tags" (
  `id` integer not null primary key autoincrement,
            movie_id INTEGER,
            tag_id INTEGER,
            movid_tagid varchar(255) UNIQUE,
            tags TEXT,
            created_at TEXT DEFAULT (datetime('now', 'localtime')), 
            updated_at TEXT DEFAULT (datetime('now', 'localtime')),
            FOREIGN KEY (tag_id) REFERENCES pbox_tags(id),
            FOREIGN KEY (movie_id) REFERENCES "pbox_movies"(id)
);
CREATE TABLE IF NOT EXISTS "pbox_movies_alts" (
  `id` integer not null primary key autoincrement,
            min_mov_id INTEGER,
            max_mov_id INTEGER,
            min_max varchar(255) UNIQUE,
            created_at TEXT DEFAULT (datetime('now', 'localtime')), 
            updated_at TEXT DEFAULT (datetime('now', 'localtime')),
            FOREIGN KEY (min_mov_id) REFERENCES "pbox_movies"(id),
            FOREIGN KEY (max_mov_id) REFERENCES "pbox_movies"(id)
);
CREATE TABLE IF NOT EXISTS "pbox_actors_movies" (
  `id` integer not null primary key autoincrement,
  `actor_id` integer,
  `movie_id` integer,
  actor_mov varchar(255) UNIQUE,
  created_at TEXT DEFAULT (datetime('now', 'localtime')),
  updated_at TEXT DEFAULT (datetime('now', 'localtime')), tags TEXT,
  foreign key(`actor_id`) references "pbox_actor"(`id`) on delete CASCADE,
  foreign key(`movie_id`) references "pbox_movies"(`id`) on delete CASCADE
);
