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'))
);
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'))
        );
CREATE TABLE thelordofporn_alias (
            actress_id INTEGER NOT NULL,
            alias TEXT NOT NULL,
            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
);
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
);
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
);
CREATE TABLE IF NOT EXISTS "javdb_actors_movies" (
            actor_id INTEGER,
            movie_id INTEGER,
            tags TEXT,
            created_at TEXT DEFAULT (datetime('now', 'localtime')),
            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 VIEW view_javdb_movies_summary AS
SELECT 
	COUNT(*) AS total_count, 
    SUM(CASE WHEN from_actor_list = 1 THEN 1 ELSE 0 END) AS from_actors,
    SUM(CASE WHEN from_movie_makers = 1 THEN 1 ELSE 0 END) AS from_makers,
    SUM(CASE WHEN from_movie_series = 1 THEN 1 ELSE 0 END) AS from_series,
    SUM(CASE WHEN from_actor_list=1 AND from_movie_makers = 1 THEN 1 ELSE 0 END) AS actor_makers,
    SUM(CASE WHEN from_actor_list=1 AND from_movie_series = 1 THEN 1 ELSE 0 END) AS actor_series,
    SUM(CASE WHEN from_movie_makers=1 AND from_movie_series = 1 THEN 1 ELSE 0 END) AS makers_series,
    SUM(CASE WHEN from_actor_list=1 AND from_movie_makers = 1 AND from_movie_series = 1 THEN 1 ELSE 0 END) AS actor_makers_series,
    SUM(CASE WHEN from_actor_list=1 AND from_movie_makers = 0 AND from_movie_series = 0 THEN 1 ELSE 0 END) AS from_actors_only,
    SUM(CASE WHEN from_actor_list=0 AND from_movie_makers = 1 AND from_movie_series = 0 THEN 1 ELSE 0 END) AS from_makers_only,
    SUM(CASE WHEN from_actor_list=0 AND from_movie_makers = 0 AND from_movie_series = 1 THEN 1 ELSE 0 END) AS from_series_only
FROM javdb_movies
/* view_javdb_movies_summary(total_count,from_actors,from_makers,from_series,actor_makers,actor_series,makers_series,actor_makers_series,from_actors_only,from_makers_only,from_series_only) */;
