<template>
	<div class="page">
		<div class="profile">
			<div class="profile-header">
				<div class="user">
					<img
						v-if="profile.avatar"
						:src="profile.avatar"
						class="avatar"
					>

					<h2 class="username ellipsis">{{ profile.username }}</h2>
				</div>

				<span class="age">{{ formatDistanceStrict(Date.now(), profile.createdAt) }}</span>
			</div>

			<nav
				v-if="profile.id === user?.id"
				class="domains"
				@click="confirmUnsaved"
			>
				<a
					:href="`/user/${profile.username}/stashes`"
					class="domain nolink"
					:class="{ active: domain === 'stashes' }"
				>Stashes</a>

				<a
					:href="`/user/${profile.username}/alerts`"
					class="domain nolink"
					:class="{ active: domain === 'alerts' }"
				>Alerts</a>

				<a
					:href="`/user/${profile.username}/summaries`"
					class="domain nolink"
					:class="{ active: domain === 'summaries' }"
				>Summaries</a>
			</nav>

			<Stashes v-if="domain === 'stashes'" />
			<Alerts v-if="domain === 'alerts' && profile.id === user?.id" />

			<Summaries
				v-if="domain === 'summaries' && profile.id === user?.id"
				:release="mockupRelease"
				@changed="(changed) => summaryChanged = changed"
			/>
		</div>
	</div>
</template>

<script setup>
import { ref, inject } from 'vue';
import { formatDistanceStrict } from 'date-fns';

import Stashes from '#/components/stashes/stashes.vue';
import Alerts from '#/components/alerts/alerts.vue';
import Summaries from '#/components/scenes/summaries.vue';

const pageContext = inject('pageContext');
const domain = pageContext.routeParams.domain;
const user = pageContext.user;
const profile = ref(pageContext.pageProps.profile);

const summaryChanged = ref(false);

const mockupRelease = {
	id: 1,
	title: 'Nut For Human Consumption',
	effectiveDate: new Date(),
	actors: [
		{
			name: 'Chanel Chakra',
			gender: 'female',
		},
		{
			name: 'Mo The Fucker',
			gender: 'male',
		},
	],
	tags: [
		{ name: 'anal' },
		{ name: 'facefucking' },
		{ name: 'deepthroat' },
		{ name: 'blowjob' },
		{ name: 'facial' },
	],
	movies: [{
		title: 'Best Of Traxxx 23',
	}],
	channel: {
		name: 'Traxxxed',
	},
	network: {
		name: 'Traxxx',
	},
};

function confirmUnsaved(event) {
	if (summaryChanged.value && !confirm('You have unchanged changes in your summary editor, are you sure you want to leave the page?')) { // eslint-disable-line no-restricted-globals, no-alert
		event.preventDefault();
	}
}
</script>

<style>
.profile-section {
	.section-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: .5rem .5rem .5rem .5rem;

		.button {
			margin-left: 1rem;
		}
	}

	.heading {
		margin: 0;
		font-size: 1.1rem;
		color: var(--primary);
	}
}

@media(--small-30) {
	.profile-section .section-header {
		padding: .5rem .5rem .5rem .5rem;
	}
}
</style>

<style scoped>
.page {
	display: flex;
	flex-grow: 1;
	justify-content: center;
	background: var(--background-base-10);
}

.profile {
	display: flex;
	flex-direction: column;
	width: 1200px;
	max-width: 100%;
	margin: 0 1rem;
}

.profile-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: .5rem 1rem;
    color: var(--highlight-strong-30);
    background: var(--shadow-strong-30);
	border-radius: 0 0 .5rem .5rem;
}

.user {
	display: flex;
	overflow: hidden;
}

.username {
	margin: 0;
	font-size: 1.25rem;
}

.age {
	display: flex;
	flex-shrink: 0;
	font-size: .9rem;

	.icon {
		width: .9rem;
		fill: var(--highlight-strong-20);
		margin-right: .75rem;
		transform: translateY(-1px);
	}
}

.avatar {
	width: 1.5rem;
	height: 1.5rem;
	border-radius: .25rem;
	margin-right: 1rem;
}

.domains {
	display: flex;
	gap: .5rem;
	padding: .5rem 0;
	margin-top: .25rem;
}

.domain {
	color: var(--glass-strong-20);
	background: var(--background-dark-20);
	padding: .5rem 1rem;
	border-radius: 1rem;
	font-size: .9rem;
	font-weight: bold;

	&.active {
		background: var(--primary);
		color: var(--text-light);
	}
}

@media(--small-30) {
	.profile {
		margin: 0 .5rem;
	}

	.age .icon {
		display: none;
	}
}

@media(--small-50) {
	.age {
		display: none;
	}
}
</style>
